AttributeError: object 'AlumniResponseFormFormSet' does not have the attribute 'new_objects'

I am using a Django admin and am trying to make some changes to a related object that appears as an InlineModelAdmin object . I am trying to do this using the save_related (self, request, form, formsets, change) method that Django provides. When I try to save something, I get an error message:

AttributeError: 'AlumniResponseFormFormSet' object has no attribute 'new_objects'

Additional Information:

1) I have two InlineModelAdmins
2) I do not save AlumniResponseInline when this error occurs. I save another InlineModelAdmin associated with the same parent model
3) Until I added the save_related () method, I had no problems saving InlineModelAdmin
4) This error occurs after all my code is executed in save_related () , so I have no control over the detection of this exception

From the documentation for save_related () :

The save_related method specifies the HttpRequest, the parent instance of ModelForm, a list of inline forms, and a boolean based on whether the parent is added or changed. Here you can perform any operations before or after saving objects associated with the parent. Note that at this point, the parent object and its form are already saved.

+5
source share
1 answer

I use save_formset instead of save_related, and I had the same problem until I realized that I missed two important lines inside the method:

instances = formset.save(commit=False)

at the beginning, and then, after the loop instances, to do something with each instance:

instance.save() #commit instance changes
formset.save_m2m() #commit the whole formset changes

in the end.

save_m2m() , formet 'new_objects', construct_change_message(self, request, form, formsets) contrib/admin/options.py

, , , - .

+2

All Articles