Changing the choices in Django Model according to a field value

davegri

If for example, I have a class Summary,

each Instance of the class Summary, can be one subject:

    subjects = (
        ('english', 'אנגלית'),
        ('bible', 'תנ"ך'),
        ('history', 'היסטוריה'),
        ('civics', 'אזרחות'),
        ('language', 'לשון'),
        ('literature', 'ספרות'),
    )


class Summary(models.Model):
...
subject = models.CharField(max_length=20, choices=subjects)
...

Now I've decided I want to hardcode some topics for each subject, so if Summary.subject = "literature" I want to add a field

subtopic = models.CharField(choices=literature_subtopics)

and make the choices equal to:

literature_subtopics = (
    ('poems', 'שירה'),
    ('short_stories', 'סיפורים קצרים'),
    ('plays', 'מחזות'),
    ('novels', 'נובלות'),
)

If the subject was English then english_subtopics would be used for the choices field.

I want to hard-code all these divisions because they will not change more than once every few years if at all, storing them in a database makes zero sense.

I need to somehow set up all these divisions for each subject, and make Django set the choices field for the subtopic appropriately.

can I override the init method to accomplish this somehow? I heard that's a bad idea and can break things.

Travis

Even if the data doesn't change often, it seems most natural to put data in the database and Python in Python files. Your proposed solution seems like you're fighting the way Django wants to do things.

What do think of a database solution?

class Subject(models.Model):
    parent = models.ForeignKey("self")
    name = models.CharField(max_length=100)
    hebrew_name = models.CharField(max_length=100)

class Summary(models.Model):
    ...
    subject = models.ForeignKey("Subject")
    ...

class SubjectForm(forms.Form):
    subject = forms.ModelChoiceField(queryset=Subject.objects.none())
    ...

    def __init__(self, *args, **kwargs): # see http://stackoverflow.com/a/4880869/1477364
        sub = kwargs.pop('parent_subject')
        super(SubjectForm, self).__init__(*args, **kwargs)
        parent_subject = Subject.objects.get(name=sub)
        sub_subjects = Subject.objects.filter(parent=parent_subject)
        self.fields['subject'].queryset = forms.ModelChoiceField(queryset=sub_subjects)

Note that the code implies there will always be a parent Subject passed to SubjectForm. (You'll need a "root" Subject.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Django annotate field value from another model

From Dev

Django model - changing options in a choices field and migrate

From Dev

Django Multiple Levels Choices for Field

From Dev

Django model field, alternative value return if null?

From Dev

Django RadioSelect Choices From Model

From Dev

django use model choices in modelform

From Dev

remembering form field value on django model form

From Dev

How to limit choices of ForeignKey choices for Django raw_id_field

From Dev

Temporary modify model's field value in Django

From Dev

return a default value for empty field on a django model

From Dev

Lazy loading a model field's choices

From Dev

Dynamically alter Field choices in Django ModelForm

From Dev

Use field value in limit_choices_to in Django

From Dev

Django field choices: Avoid repeating value and human-readable form?

From Dev

Choosing a method based on a field value in a Django model

From Dev

Changing model field within the Django Shell

From Dev

One Django model field as array of strings and another field as dropdown with choices from first field?

From Dev

Convert a Django Model Field Choices into a JSON

From Dev

What is Django model field choices best practices?

From Dev

How to get the value of a Django Model Field object

From Dev

remembering form field value on django model form

From Dev

Lazy loading a model field's choices

From Dev

How to get the Django model with maximum field value?

From Dev

Django field choices not properly updating

From Dev

django - aggregate data according to field in model

From Dev

django get default model field value

From Dev

Django model with multiple choices

From Dev

django - admin model assign value to limit_choices_to from another field inside the model

From Dev

Create instance of model with field with choices

Related Related

  1. 1

    Django annotate field value from another model

  2. 2

    Django model - changing options in a choices field and migrate

  3. 3

    Django Multiple Levels Choices for Field

  4. 4

    Django model field, alternative value return if null?

  5. 5

    Django RadioSelect Choices From Model

  6. 6

    django use model choices in modelform

  7. 7

    remembering form field value on django model form

  8. 8

    How to limit choices of ForeignKey choices for Django raw_id_field

  9. 9

    Temporary modify model's field value in Django

  10. 10

    return a default value for empty field on a django model

  11. 11

    Lazy loading a model field's choices

  12. 12

    Dynamically alter Field choices in Django ModelForm

  13. 13

    Use field value in limit_choices_to in Django

  14. 14

    Django field choices: Avoid repeating value and human-readable form?

  15. 15

    Choosing a method based on a field value in a Django model

  16. 16

    Changing model field within the Django Shell

  17. 17

    One Django model field as array of strings and another field as dropdown with choices from first field?

  18. 18

    Convert a Django Model Field Choices into a JSON

  19. 19

    What is Django model field choices best practices?

  20. 20

    How to get the value of a Django Model Field object

  21. 21

    remembering form field value on django model form

  22. 22

    Lazy loading a model field's choices

  23. 23

    How to get the Django model with maximum field value?

  24. 24

    Django field choices not properly updating

  25. 25

    django - aggregate data according to field in model

  26. 26

    django get default model field value

  27. 27

    Django model with multiple choices

  28. 28

    django - admin model assign value to limit_choices_to from another field inside the model

  29. 29

    Create instance of model with field with choices

HotTag

Archive