Django - 模型选择 [英] Django - Choices for Models

查看:197
本文介绍了Django - 模型选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索和查看文档,但是我想在这里询问并确认最佳解决方案。

I have been searching and looking through docs, but I want to ask and confirm for the best solution here.

尝试定义模型选择。


  1. '是的,不,不确定'选择从收音机选择

  2. 多个选择

简单示例:
在我的models.py中,我有

Simple Example: In my models.py, I have

class Property(models.Model):
    name = models.CharField()

class Feature(models.Model):
    YES_CHOICES = (       # example of 1, there can be only one selection
        ('YES', 'Yes'),
        ('NO', 'No'),
        ('NOT_SURE', 'Not Sure')
    )
    PARKING_CHOICES = (    # example of 2, there can be multiple selections
        ('GARAGE', 'Garage'),
        ('STREET', 'Street'),
        ('PRIVATE_LOT', 'Private Lot'),
        ('VALET', 'Valet'),
    )

    nearby_school = models.CharField(max_length=8, choices=YES_CHOICES)
    parking_options = models. MultipleChoiceField(choices=PARKING_CHOICES)

class PropertyFeature(models.Model)
    property = models.ForeignKey(Property)
    feature = models.ForeignKey(Feature)
    ...

是最好的方法吗?


  1. 我应该使用NullBooleanField代替yes,否,不确定的问题?

  2. 这是定义和存储多种选择答案的正确方法吗?有时,我看到人们使用manytomany对象。

只需使用Django提供的最有效率和最简单的方法。

Just want to use the most efficient and the easiest method offered from Django.

推荐答案

是的, NullBoolean 是适当的,但如果有更多的选项不符合 NullBoolean ,我赞成 IntegerField ,以便在各个选项之间进行可读性和一致性。

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 可以直观地表示 n / a ,但是当您添加更多单选题时,我认为甚至更直观地使用映射到静态变量的 IntegerField

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.

对于这种类型的场景,用户可能会基于这些功能的过滤器属性,在动态查询中不需要特殊情况Null是有用的。

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.

示例:

...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)

古代邮政仍然是一个很好的参考:
http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/

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.
    )

对于多选字段,我确实认为使用m2m字段是最简单/最好的方法。

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

您可以设置您的 forms.MultipleChoiceField 将数据存储为逗号分隔字段&显示适当,但事实上,您可以轻松地查询m2m字段是一个巨大的好处+它正好开箱即用, ModelMultipleChoiceField

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.

这篇关于Django - 模型选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆