I am reading the save_model
and save_formset
in django admin from here
https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
But I am confused as to when save_formset
is called.
Shouldn't the formset be saved during save_model
?
By default Django admin interface provides two types of inlines to edit models on the same page as a related model – StackedInline and TabularInline . StackedInline is mostly used when there are not so many objects. If number of models is rather big, TabularInline can help you.
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.
To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.
The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.
The documentation could use a little clarification.
save_model
is called during add or change to save the base model. It is specifically not intended to save related or inlined models.save_related
is called (once) after save_model
completes to save off all related / inlined models.save_formset
is called potentially many times during each add / change, once for every inline defined on your ModelAdmin
. It is called by the base class implementation of save_related
.If you look at the code for admin.ModelAdmin
, you can see that save_model
and save_related
happen one after the other:
class ModelAdmin(BaseModelAdmin):
def changeform_view(...):
# ...
if all_valid(formsets) and form_validated:
self.save_model(request, new_object, form, not add)
self.save_related(request, form, formsets, not add)
and that save_related
has a simple implementation to call save_formset
for each inline:
class ModelAdmin(BaseModelAdmin):
# ...
def save_related(self, request, form, formsets, change):
form.save_m2m()
for formset in formsets:
self.save_formset(request, form, formset, change=change)
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