排除和重新定位表单中的字段

时间:2012-06-26 10:49:58

标签: django

我已经排除了这样的字段,以便设置它的标签。

class ContactsForm(ModelForm):
    marital_status    = forms.ModelChoiceField(label=_(u'Marital Status'), queryset=MaritalStatus.objects.all())

class Meta:
        model = Contact
        exclude = ('marital_status',)

这是有效的,但婚姻状况现在出现在所有其他元素之后的表格的最末端。

有没有办法指定字段的位置?

或者我甚至不必首先排除该字段,如果我能够自己在模型中设置标签,就像下面的示例一样,我在其中设置标签“出生日期”。我似乎无法为models.ForeignKey设置标签,任何想法?

marital_status      = models.ForeignKey(MaritalStatus)
birth_date          = models.DateField(_(u"Day Of Birth"), blank=True)

2 个答案:

答案 0 :(得分:1)

根本不需要排除它。只需在第一个示例中声明它,并将其保留在exclude参数之外。

答案 1 :(得分:1)

您可以在定义外键时使用'verbose_name'属性,该外键将在表单中显示为标签。

参考:https://docs.djangoproject.com/en/dev/topics/db/models/#verbose-field-names

marital_status = models.ForeignKey(MaritalStatus, verbose_name=_(u'Marital Status'))