我知道我能够过滤Foreignkey或Many2ManyFields的查询集,但是,我如何为一个简单的CharField做一个选择窗口小部件(选择标记)。
例如:
PRODUCT_STATUS = (
("unapproved", "Unapproved"),
("approved", "Listed"),
#("Backorder","Backorder"),
#("oos","Out of Stock"),
#("preorder","Preorder"),
("userdisabled", "User Disabled"),
("disapproved", "Disapproved by admin"),
)
和Field:
o_status = models.CharField(max_length=100, choices=PRODUCT_STATUS, verbose_name="Product Status", default="approved")
假设我希望将其限制为“已批准”和“userdisabled”而是显示完整数组(这是我想在管理员中显示的内容),我该怎么做?
谢谢!
答案 0 :(得分:0)
class YourModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(YourModelForm, self).__init__(*args, **kwargs)
self.fields['your_field'].choices = (('a', 'A'), ('b', 'B'))
class Meta:
model = YourModel
我想这与从ForeignKey或M2M属性覆盖查询集没有什么不同。
PS:感谢irc.freenode.net上#django的内容