1
我有一个外键引用,它显示为客户端上的选择框,但它已预先填充值。我希望选择框在显示时为空,因为它将由Ajax调用填充。删除外键选择字段中的所有元素
这是我的模型
class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
serving_size = models.ForeignKey(ServingSize)
quantity = models.IntegerField()
order = models.IntegerField()
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
,这是我的模型形式
class RecipeIngredientForm(forms.ModelForm):
class Meta:
model = RecipeIngredient
fields = ('ingredient', 'quantity', 'serving_size')
widgets = {
'ingredient': forms.TextInput(attrs={'class' : 'recipe_ingredient'}),
'quantity': forms.TextInput(),
'serving_size' : forms.ChoiceField(choices=PLEASE_SELECT, widget=forms.Select()),
}
我希望“serving_size”现场有从数据库中指定我的选择,而不是任何数据。显然,我得到一个错误
AttributeError: 'ModelChoiceField' object has no attribute 'to_field_name'
任何想法?
感谢您的答复。如果我没有将它包含在域中,它不会显示在页面上。我试过这 fields =('成分','数量') serving_size = forms.ChoiceField() – iJK 2010-07-27 22:55:30
我已经添加了一个完整的表单。你已经尝试过吗?我很确定这应该起作用。 – gruszczy 2010-07-28 00:15:26
感谢它的工作。如果你不介意你能解释我们做了什么吗?我们刚创建了一个新领域吗? – iJK 2010-07-28 01:20:23