我一直在搜索和浏览文档,但我想在这里询问并确认最佳解决方案。
试图定义模型选择。
- “是,否,不确定”来自 Radio Select 的选择
- 我将如何定义多项选择
简单示例:在我的 models.py 中,我有
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)
...
这些是最好的方法吗?
- 我应该使用 NullBooleanField 代替是、否、不确定的问题吗?
- 这是定义和存储多项选择答案的正确方法吗?有时,我看到人们使用多对多对象。
只想使用 Django 提供的最有效和最简单的方法。