在django中隐藏表单中的模型字段

时间:2015-03-30 23:35:35

标签: django

我想从表单中隐藏一个模型字段,这是一个自动提交的时间段。

check_out_time = models.DateTimeField(default = datetime.now() + timedelta(minutes = 60))
first_name = models.CharField(max_length = 120)
last_name = models.CharField(max_length = 120)

我想只在表单中显示名字和姓氏,而不是结帐时间。请帮帮我,如何隐藏django中的任何字段?

1 个答案:

答案 0 :(得分:0)

请参阅https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#selecting-the-fields-to-use

class YourForm(forms.ModelForm):

    class Meta:
        model = YourModel
        exclude = ('check_out_time', )

<强>更新

如果您想有条件地排除某个字段:

class YourForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):    
        super(YourForm, self).__init__(*args, **kwargs)
        if <your logic here>:
           del self.fields['check_out_time']

    class Meta:
        model = YourModel