gpt4 book ai didi

python - 如何以编程方式在 Django 的 ModelForm 中设置字段?

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:01 26 4
gpt4 key购买 nike

来自 this question我想将我的表单从常规 Form 转换为 ModelForm,这样我就可以利用 ModelForm 中的 instance 参数。

这是我当前的表单代码:

class OrderDetailForm(forms.Form):
def __init__(
self,
user,
can_edit_work_type=None,
can_edit_vendor=None,
can_edit_note=None,
*args,
**kwargs
):
super(OrderDetailForm, self).__init__(*args, **kwargs)

if can_edit_work_type:
self.fields['work_type'] = forms.ChoiceField(choices=Order.WORK_TYPE_CHOICES)
if can_edit_vendor:
self.fields['vendor'] = forms.ModelChoiceField(
queryset=Vendor.objects.all(),
empty_label="Choose a vendor",
)
if can_edit_note:
self.fields['note'] = forms.CharField(widget=forms.Textarea)

def clean(self):
super(OrderDetailForm, self).clean()

if 'note' in self.cleaned_data:
if len(self.cleaned_data['note']) < 50:
self._errors['note'] = self.error_class([u"Please enter a longer note."])

del self.cleaned_data['note']

return self.cleaned_data

如您所见,我有一些 if 语句来确定字段是否甚至在表单中显示您(从逻辑上讲,这意味着某些用户只能编辑字段的某些部分)。

我如何在 ModelForm 中做到这一点?我将 fields 理解为一个元组,因此不能像我在 Form 中那样附加它。所以我想做类似的事情

class OrderDetailForm(forms.ModelForm):
class Meta:
model = Order
# fields = ('work_type', 'vendor', 'note') I can't do that since I need to be able to control it. See below.

# Can I control widgets even if that field doesn't exist?
widgets = {
'note': forms.Textarea(),
}

def __init__(
self,
user,
can_edit_work_type=None,
can_edit_vendor=None,
can_edit_note=None,
*args,
**kwargs
):
super(OrderDetailForm, self).__init__(*args, **kwargs)

fields = []

if can_edit_work_type:
fields.append('work_type')
if can_edit_vendor:
fields.append('vendor')
if can_edit_note:
fields.append('note')

self.Meta.fields = tuple(fields) # Does this work?

def clean(self):
super(OrderDetailForm, self).clean()

if 'note' in self.cleaned_data:
if len(self.cleaned_data['note']) < 50:
self._errors['note'] = self.error_class([u"Please enter a longer note."])

del self.cleaned_data['note']

return self.cleaned_data

这可能吗?您如何控制 ModelForm 中的字段?

最佳答案

另一种可能的方法是在 View 中生成一个内联表单类,以根据请求排除字段,例如,为 Order 模型定义一个普通模型表单,称为 OrderDetailForm:

class OrderDetailForm(forms.ModelForm):
class Meta:
model = Order
fields = ('work_type', 'vendor', 'note')
widgets = {
'note': forms.Textarea(),
}

在 View 中,例如编辑订单,根据OrderDetailForm创建一个自定义的表单:

def edit(request, order_id):
order = Order.objects.get(pk=order_id)
can_edit_work_type = bool(request.REQUEST.get('can_edit_work_type', False))
can_edit_vender = bool(request.REQUEST.get('can_edit_vender', False))
can_edit_note = bool(request.REQUEST.get('can_edit_note', False))

exclude_fields = []

if not can_edit_work_type:
exclude_fields.append('work_type')

if not can_edit_vender:
exclude_fields.append('vender')

if not can_edit_note:
exclude_fields.append('note')

class CustomizedOrderForm(OrderDetailForm):
class Meta:
model = Order
exclude = tuple(exclude_fields)

if request.method == 'POST':
form = CustomizedOrderForm(instance=order, data=request.POST)
if form.is_valid():
form.save()
else:
form = CustomizedOrderForm(instance=order)
return render(request, 'order_form.html', {'form': form})

关于python - 如何以编程方式在 Django 的 ModelForm 中设置字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10645733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com