![](/img/trans.png)
[英]how can I make a Django model form with a field name in the form different from the model field name?
[英]How can I disable a model field in a django form
我有这样的模型:
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
但是用户仍然可以更改该字段。
什么是实现我追求的真正方式?
第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:确保在后端有效禁用该字段
覆盖您的字段的干净方法,以便无论POST输入(有人可以伪造POST,编辑原始HTML等),您都可以获得已存在的字段值。
def clean_status(self):
# when field is cleaned, we always return the existing model field.
return self.instance.status
您是否尝试过使用排除功能?
这样的事情
class PartialAuthorForm(ModelForm):
class Meta:
model = Author
fields = ('name', 'title')
class PartialAuthorForm(ModelForm):
class Meta:
model = Author
exclude = ('birth_date',)
只需为状态字段自定义窗口小部件实例:
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文档
来自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
有一种非常简单的方法:
class GenerateCertificate(models.Model):
field_name = models.CharField(
max_length=15,
editable=False)
def __unicode__(self):
return unicode(self.field_name)
editable=False
将禁用该字段以进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.