Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

passing initial data to a formset, doesn't show data_changed as true / does not save data

See original GitHub issue

Not sure if this is a deeper problem with Django ModelForms, or just this package.

If I have a ModelFormSetView, to add Persons objects to a Trip object. I want to add some initial data to the forms, default names for each person, “Person 1”, “Person 2” etc.

class AddPersonsFormSetView(ModelFormSetView):
    model = Person
    template_name = 'AddPersons.html'
    extra = 1
    success_url = '/trip/%s/expense/add/'

     def get_formset_kwargs(self):
         kwargs = super(AddPersonsFormSetView, self).get_formset_kwargs()
         num_persons = self.kwargs['num_persons']

         ## inital data will give the name Person <increment> 
         initial = [ 
             { 'name' : "Person %s" % i , 'trip' : self.kwargs['trip_id'] }  for i in range( 1, int(num_persons)+ 1)

         kwargs.update({'initial': initial })
         return kwargs

Now if I change the values in the form, it saves correctly. If I leave the default values as they are, the PersonForm generated by the factory does not see the data_changed() as True, so doesn’t save anything.

I can work around this by creating the form, overriding the has_changed method, and specifying it in the get_factory_kwargs() method of the AddPersonFormSetView but this isn’t an obvious solution until you step through the code. It doesn’t not feel seem correct behaviour to ignore default values.

class PersonForm(ModelForm):
    class Meta:

    def has_changed(self):
        Overriding this, as the initial data passed to the form does not get noticed, 
        and so does not get saved, unless it actually changes
        changed_data = super(ModelForm, self).has_changed()
        return bool(self.initial or changed_data)

class AddPersonsFormSetView(ModelFormSetView):
    def get_factory_kwargs(self):
        kwargs = super(AddPersonsFormSetView, self).get_factory_kwargs()
        kwargs['form'] = PersonForm
        return kwargs

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8

github_iconTop GitHub Comments

jonashaagcommented, Oct 2, 2015

Happened to me too. The correct way to fix this is to not pass the initial argument to the form in case we’re binding the form (POST).

jonashaagcommented, Nov 16, 2018


Read more comments on GitHub >

github_iconTop Results From Across the Web

Django formset data does not get saved when initial values ...
I had a similar issue using django extra views. I solved it bay modifying the form class that is passed to the inlineformset_factory....
Read more >
Formsets - Django documentation
We passed in no data to the formset which is resulting in a valid form. The formset is smart enough to ignore extra...
Read more >
Formset: Saving Unchanged data : r/django - Reddit
Given this, the form will not save because it is not valid; it is missing the last_name. I believe setting 'has_changed' = True...
Read more >
Storage - App Inventor - Massachusetts Institute of Technology
By default data will be stored in a server maintained by MIT, however you can setup and run ... Legacy functionality will not...
Read more >
Angular Data Grid: Change Detection
This means the grid will automatically keep aggregation results (the values in the grouped row) up to date as the data beneath it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found