Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you modify form data before saving it while using Django's CreateView?

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()
like image 581
enchance Avatar asked Dec 12 '18 11:12

enchance


3 Answers

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
like image 151
Daniel Roseman Avatar answered Oct 23 '22 01:10

Daniel Roseman


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

like image 26
Sachin Avatar answered Oct 23 '22 01:10

Sachin


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())
like image 43
lorey Avatar answered Oct 23 '22 02:10

lorey