Grouped CATEGORY_CHOICES with more then one level sub choices?

Viewed 354

Is it possible to have to organized choice to have more then one levels in the named groups.

Or should I do this a completely different way not using choices.

Django documents only have documentation on one level deep which I was able to get working.enter image description here

https://docs.djangoproject.com/en/3.0/ref/models/fields/

The first element in each tuple is the name to apply to the group. The second element is an iterable of 2-tuples, with each 2-tuple containing a value and a human-readable name for an option. Grouped options may be combined with ungrouped options within a single list (such as the 'unknown' option in this example).

I want to create a class so when I add a meteorites it goes into the proper classification but also inherits the top levels. For example a shergottites meteorite is also a differentiated meteorites, martian meteorites.

  • differentiated meteorites
    • martian
      • shergottites

I found this on Django project which is close to what I need but there are a few extra levels of organization.

MEDIA_CHOICES = [
    ('Audio', (
            ('vinyl', 'Vinyl'),
            ('cd', 'CD'),
        )
    ),
    ('Video', (
            ('vhs', 'VHS Tape'),
            ('dvd', 'DVD'),
        )
    ),
    ('unknown', 'Unknown'),
]

Below is what I am trying to do which is to have two broad categories with 3 and 4 sub categories and inside those they each might have up to two more subcategories.

Later this grouping will be import for sorting, searching and most functionality.

Here is NASA chart I am using for classification as a picture

NASA Meteorite Classification Chart

In text

undifferentiated meteorites
    chodrites
        carbonaceous
            CB
            CH
            CK
            CM
            CR
            CV
            CO
            CI

        ordinary
            H
            L
            LL

        rumuruti
            R

        enstatite
            EH
            EL

differentiated meteorites

    achodrites
        primitive achodrites
            iodranities
            acapulcoites
            winonaites
        martian
            shergottites
            nakhites
            chassignites
            ALH 84001 opx
        aubrites
        urelities
        HED
            eurites
            diogenites
            howardites
        angrites
        brachinites
        lunar
            feldspathic breccia
            basaltic
            polymict

    stony iron meteorites
        pallasite
        mesosiderites

    iron metoerites
        IAB
        IIAB
        IIIAB
        IVAB
        Ungrouped
        Other

I found this on Django project which is close to what I need but there are a few extra levels of organization.

MEDIA_CHOICES = [
    ('Audio', (
            ('vinyl', 'Vinyl'),
            ('cd', 'CD'),
        )
    ),
    ('Video', (
            ('vhs', 'VHS Tape'),
            ('dvd', 'DVD'),
        )
    ),
    ('unknown', 'Unknown'),
]

This is what I was able to achieve but its a full level off from the desired result.

from django.db import models
from django.utils import timezone


class Post(models.Model):
    CATEGORY_CHOICES = [
        ('iron metoerites', (
            ('IAB', 'IAB'),
            ('IIAB', 'IIAB'),
            ('IIIAB', 'IIIAB'),
            ('IVAB', 'IVAB'),
            ('Ungrouped', 'Ungrouped'),
            ('Other', 'Other'),
            )
         ),
        ('stony meteorites', 'stony meteorites'),
        ('primitive achodrites', (
            ('iodranities', 'iodranities'),
            ('acapulcoites', 'acapulcoites'),
            ('winonaites', 'winonaites'),
            )
         ),
        ('carbonaceous', (
            ('CB', 'CB'),
            ('CH', 'CH'),
            ('CK', 'CK'),
            ('CM', 'CM'),
            ('CR', 'CR'),
            ('CV', 'CV'),
            ('CO', 'CO'),
            ('CI', 'CI'),
            )
         ),
        ('ordinary', (
            ('H', 'H'),
            ('L', 'L'),
            ('LL', 'LL'),
            )
         ),
        ('rumuruti', (
            ('R', 'R'),
            )
         ),      
        ('enstatite', (
            ('EH', 'EH'),
            ('EL', 'EL'),
            )
        ),
        ('martian', (
            ('shergottites', 'shergottites'),
            ('nakhites', 'nakhites'),
            ('chassignites', 'chassignites'),
            ('ALH84001opx', 'ALH 84001 opx'),
            )
         ),
        ('HED', (
            ('eurites', 'eurites'),
            ('diogenites', 'diogenites'),
            ('howardites', 'howardites'),
            )
         ),
        ('angrites', 'angrites'),
        ('brachinites', 'brachinites'),
        ('lunar', (
            ('feldspathic_breccia', 'feldspathic breccia'),
            ('basaltic', 'basaltic'),
            ('polymict', 'polymict'),
            )
             ),
        ('stony-iron meteorites', (
            ('pallasite', 'pallasite'),
            ('mesosiderites', 'mesosiderites'),
            ),
         ),
        ]

    main_image = models.FileField(upload_to='media/', default='')
    second_image = models.FileField(upload_to='media/', default='')
    author = models.CharField(max_length=256, default='')
    title = models.CharField(max_length=256, default='')
    description = models.TextField(default='')
    more_info = models.TextField(default='')
    l_heading = models.CharField(max_length=256, default='')
    l_heading_text = models.CharField(max_length=256, default='')
    qoute = models.CharField(max_length=256, default='')
    qoute_name = models.CharField(max_length=256, default='')
    s_heading = models.CharField(max_length=256, default='')
    s_heading_text = models.CharField(max_length=256, default='')
    category = models.CharField(
        max_length=256, choices=CATEGORY_CHOICES, default='stony meteorites')
    created_date = models.DateTimeField(default=timezone.now, blank=True)
    text = models.TextField(default='')
    tag_1 = models.CharField(max_length=256, default='')
    tag_2 = models.CharField(max_length=256, default='')
    tag_3 = models.CharField(max_length=256, default='')
    class Meta:
        ordering = ['-created_date']
    def __str__(self):
        return self.title

I am self taught through basic Django tutorials and basic google search troubleshooting which has worked so till this. I am open to any solution, but was wondering whats the best practice for adding something with multiple nesting inside.

0 Answers
Related