我的Django CBV CreateView中使用了以下模型:
class Agreement(BaseModel):
offer = models.ForeignKey(Offer, null=True, blank=True)
owner_name = models.CharField(), max_length=15, null=True, blank=False)
has_second_owner = models.BooleanField(default=False)
second_owner_name = models.CharField(max_length=15, null=True, blank=True)
并形成:
class AgreementForm(BootstrapFormMixin, forms.ModelForm):
class Meta:
model = Agreement
fields = ('owner_name', 'has_second_owner', 'second_owner_name')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-5'
self.helper.field_class = 'col-sm-7'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.form_id = self.__class__.__name__.lower()
self.helper.layout = Layout(
Field('owner_name'),
Field('has_second_owner'),
Field('second_owner_name'),
)
我正在使用CreateView来创建协议模型的实例。默认情况下,不需要second_owner_name
(因为has_second_owner
为假)。
如果用户更改has_second_owner
字段,我必须修改视图中所需的second_owner_name
字段。
我该怎么做?根据POST数据修改字段的正确方法是什么?谢谢