我是django新手所以请原谅我的基本问题 我的模型中有一个具有ForeignKey的类。我正在使用django.views.generic.UpdateView来编辑字段。对于ForeignKey,显示一个组合框,这正是我想要的,但引用表的所有字段都出现在组合框中。我想在组合框中只显示2,例如:“field1 [field2]” 我该如何控制这种行为?
谢谢,
诉
答案 0 :(得分:0)
这是因为:
<div class="test left">
testing something
</div>
(模型字段)默认为.test {
position: fixed;
background: green;
color: #fff;
top: 0;
left: 0;
}
.left {
left: -100%;
}
(表单字段)。ForeignKey
&#39; ModelChoiceField
所有模型对象。如果您想更改此行为并仅提供少数选定的模型对象供用户在ModelChoiceField
中进行选择,则必须自行覆盖此字段queryset
。例如:
ModelChoiceField
然后,您可以在queryset
中指定class YourForm(forms.ModelForm):
class Meta:
model = YourModel
your_foreignkey_field = forms.ModelChoiceField(queryset=YourOtherModel.objects.filter(some_criteria))
。
答案 1 :(得分:0)
我所要做的就是为我的模型类定义__str__
函数。
class Systm(ParentModel):
...
def __str__(self):
return "%s [%s]" % (self.name, self.type)