I'm using the CreateView
of Django and I'm trying to find out how I can modify any text which gets sent before it gets saved. For example, right now I'm only looking to lowercase all the text before saving.
I know I need to use form_valid()
but I can't seem to get it right.
forms.py
class ConfigForm(forms.ModelForm):
class Meta:
model = Config
fields = ["heading", "name", "data", "rating"]
views.py
def form_valid(self, form):
super().form_valid(form)
form.fields["heading"].lower()
form.fields["name"].lower()
form.fields["data"].lower()
That shouldn't be done in form_valid. You should do that in the form itself. Instead of letting CreateView automatically create a form for you, do it explicitly and overwrite the clean
method.
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('list', 'of', 'fields')
def clean(self):
for field, value in self.cleaned_data.items():
self.cleaned_data['field'] = value.lower()
...
class MyCreateView(views.CreateView):
form_class = MyForm
Override get_form_kwargs
method to update the kwargs
which instantiates the form.
Solution:
def get_form_kwargs(self):
# update super call if python < 3
form_kwargs = super().get_form_kwargs()
form_kwargs['data']['str_field_name'] = form_kwargs['data']['str_field_name'].lower()
return form_kwargs
Ref: get_form_kwargs docs
While it may not be the nicest solution, it can be done like this:
def form_valid(self, form):
self.object = form.save(commit=False)
# ...
self.object.save()
return http.HttpResponseRedirect(self.get_success_url())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With