如何禁用django表单中的模型字段

时间:2011-02-09 13:56:03

标签: python django django-forms

我有一个这样的模型:

class MyModel(models.Model):
    REGULAR = 1
    PREMIUM = 2
    STATUS_CHOICES = ((REGULAR, "regular"), (PREMIUM, "premium"))
    name = models.CharField(max_length=30)
    status = models.IntegerField(choices = STATUS_CHOICES, default = REGULAR)

class MyForm(forms.ModelForm):
    class Meta:
        model = models.MyModel

在视图中,我初始化一个字段并尝试使其不可编辑:

myform = MyForm(initial = {'status': requested_status})
myform.fields['status'].editable = False

但是用户仍然可以更改该字段。

实现我追求的目标的真正方法是什么?

5 个答案:

答案 0 :(得分:37)

第1步:停用前端小部件

使用HTML readonly属性:
http://www.w3schools.com/tags/att_input_readonly.asp

disabled属性:
http://www.w3.org/TR/html401/interact/forms.html#adef-disabled

您可以通过widget attrs属性注入任意HTML键值对:

myform.fields['status'].widget.attrs['readonly'] = True # text input
myform.fields['status'].widget.attrs['disabled'] = True # radio / checkbox

第2步:确保在后端有效禁用该字段

覆盖你的字段的clean方法,这样无论POST输入(有人可以伪造POST,编辑原始HTML等),你都会获得已经存在的字段值。

def clean_status(self):
    # when field is cleaned, we always return the existing model field.
    return self.instance.status

答案 1 :(得分:6)

您是否尝试过使用排除功能?

类似这样的事情

class PartialAuthorForm(ModelForm):
class Meta:
    model = Author
    fields = ('name', 'title')

class PartialAuthorForm(ModelForm):
class Meta:
    model = Author
    exclude = ('birth_date',)

Reference Here

答案 2 :(得分:4)

只需为状态字段自定义窗口小部件实例:

class MyModel(models.Model):
    REGULAR = 1
    PREMIUM = 2
    STATUS_CHOICES = ((REGULAR, "regular"), (PREMIUM, "premium"))
    name = models.CharField(max_length=30)
    status = models.IntegerField(choices = STATUS_CHOICES, default = REGULAR)

class MyForm(forms.ModelForm):
    status =  forms.CharField(widget=forms.TextInput(attrs={'readonly':'True'}))

    class Meta:
        model = models.MyModel

请参阅:Django Documentation

答案 3 :(得分:4)

来自django 1.9:

from django.forms import Textarea

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = '__all__'
        widgets = {'my_field_in_my_model': Textarea(attrs={'cols':80,'rows':1}),}             

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['my_field_in_my_model'].disabled = True

答案 4 :(得分:1)

有一个非常简单的方法:

class GenerateCertificate(models.Model):

    field_name = models.CharField(
        max_length=15,
        editable=False)
    def __unicode__(self):
        return unicode(self.field_name)

editable=False将使该字段无法编辑。