Django - Choices for Models

时间秒杀一切 提交于 2019-12-04 17:23:38

Yes, NullBoolean is appropriate, but if there are more options that don't fit the profile of NullBoolean, I'm in favor of IntegerField for readability and consistency across options.

Null could intuitively mean n/a, but as you add more single choice questions, I think it's even more intuitive to use an IntegerField mapped to static variables.

Also for this type of scenario where the user will probably filter properties based on these features, it's useful not to have to special case Null in your dynamic query.

Example:

...filter(Q(nearby_school__isnull=True) | Q(nearby_school='NO')),
    other_choice='SOME_CHOICE')
# vs
...filter(Q(nearby_school=Feature.NOT_SURE) | Q(nearby_school=Feature.NO)), 
    other_choice=Feature.SOME_CHOICE)

This ancient post still serves as a great reference: http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/

class Feature(models.Model):
    YES = 0
    NO = 1
    NOT_SURE = 2
    SOMETIMES = 3
    YES_CHOICES = ( 
        (YES, 'Yes'),
        (NO, 'No'),
        (NOT_SURE, 'Not Sure'),
        (SOMETIMES, 'Sometimes'), # extensible.
    )

As for a multiple choice field, I do think using a m2m field is the easiest/best way.

You could set up your forms.MultipleChoiceField to store data as a comma separated field & display appropriately, but the fact that you can query the m2m field easily is a huge benefit + it works right out of the box with ModelMultipleChoiceField.

18 months or so later, there is now a better way of dealing with choices in Django; Łukasz Langa's dj.choices. An example of its use, from the blog post introducing the project:

from dj.choices import Choices, Choice

class Gender(Choices):
    male = Choice("male")
    female = Choice("female")
    not_specified = Choice("not specified")

class User(models.Model):
    gender = models.IntegerField(choices=Gender(),
            default=Gender.not_specified.id)

    def greet(self):
        gender = Gender.from_id(self.gender)
        if gender == Gender.male:
            return 'Hi, boy.'
        elif gender == Gender.female:
            return 'Hello, girl.'
        else:
            return 'Hey there, user!'

This still won't work for multiple selections, though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!