Django 中模型的选择
我有一个模型,它可以选择 CHOICES 中定义的选项,如下所示:
CHOICES = (
('1', '1'),
('2', '2'),
('3', '3'),
)
class select(models.Model):
first = models.CharField(max_length=3, choices=CHOICES)
second = models.CharField(max_length=3, choices=CHOICES)
我希望能够在管理页面中添加、删除或更改选项。所以我的方法是以选择作为模型,但我不知道这是否是正确的方法。如果是的话我该如何设置呢?谢谢。
I have a model that picks up the choices defined in CHOICES like this:
CHOICES = (
('1', '1'),
('2', '2'),
('3', '3'),
)
class select(models.Model):
first = models.CharField(max_length=3, choices=CHOICES)
second = models.CharField(max_length=3, choices=CHOICES)
I would like to be able to add, remove or change choices in the admin page. So my approach would be to have CHOICES as a model, but I dont know if this is the right way to do this. If it is, then how can I set it? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要类似 ManyToMany 字段的内容,而不是字符字段上的选项。更多信息在这里:http://docs。 djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField
You want something like a ManyToMany field, not choices on a Character field. More info here: http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField