I have a django form and on my view function I do this :
我有一个django表单,在我的视图函数中我这样做:
search_packages_form = SearchPackagesForm( data = request.POST )
I would like to overwrite a form field called price which is decleared as such :
我想覆盖一个名为price的表单字段,该字段被删除:
price = forms.ChoiceField( choices = PRICE_CHOICES, required = False,widget = forms.RadioSelect )
I would like to overwrite the form field before calling search_packages_form.is_valid()
我想在调用search_packages_form.is_valid()之前覆盖表单字段
I thought of doing :
我想到了:
search_packages_form.data['price'] = NEW_PRICE
But it does not work. Any ideas ?
但它不起作用。有任何想法吗 ?
2 个解决方案
#1
2
Probably not the Django way but based on https://*.com/a/17304350/2730032 I'm guessing the easiest way to change your form value before validation is to do something like the following:
可能不是Django方式,但基于https://*.com/a/17304350/2730032我猜测在验证之前更改表单值的最简单方法是执行以下操作:
def search_packages_view(request):
if request.method == 'POST'
updated_request = request.POST.copy()
updated_request.update({'price': NEW_PRICE})
search_packages_form = SearchPackagesForm(request.POST)
if search_packages_form.is_valid():
# You're all good
This works but I'd be interested if anyone has another way that seems more in line with Django, or if there isn't: then an explanation about why.
这有效,但我感兴趣的是,如果有人有另一种方式似乎更符合Django,或者如果没有:那么解释为什么。
#2
-1
form.is_valid()
runs through your form's (and model's in case of a ModelForm
) clean
method's, returning True
or False
form.is_valid()运行你的表单(和ModelForm的模型)clean方法,返回True或False
If you plan on changing form data you can do it within the general clean method or at field level, e.g.
如果您计划更改表单数据,可以在常规清理方法或字段级别执行此操作,例如
class YourForm(DerivingClass):
# regular stuff
def clean_<ATTR>(self):
# here
return self.cleaned_data
def clean(self):
# or here
return super(YourForm, self).clean()
#1
2
Probably not the Django way but based on https://*.com/a/17304350/2730032 I'm guessing the easiest way to change your form value before validation is to do something like the following:
可能不是Django方式,但基于https://*.com/a/17304350/2730032我猜测在验证之前更改表单值的最简单方法是执行以下操作:
def search_packages_view(request):
if request.method == 'POST'
updated_request = request.POST.copy()
updated_request.update({'price': NEW_PRICE})
search_packages_form = SearchPackagesForm(request.POST)
if search_packages_form.is_valid():
# You're all good
This works but I'd be interested if anyone has another way that seems more in line with Django, or if there isn't: then an explanation about why.
这有效,但我感兴趣的是,如果有人有另一种方式似乎更符合Django,或者如果没有:那么解释为什么。
#2
-1
form.is_valid()
runs through your form's (and model's in case of a ModelForm
) clean
method's, returning True
or False
form.is_valid()运行你的表单(和ModelForm的模型)clean方法,返回True或False
If you plan on changing form data you can do it within the general clean method or at field level, e.g.
如果您计划更改表单数据,可以在常规清理方法或字段级别执行此操作,例如
class YourForm(DerivingClass):
# regular stuff
def clean_<ATTR>(self):
# here
return self.cleaned_data
def clean(self):
# or here
return super(YourForm, self).clean()