Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the diff between save_model and save_formset in django admin

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?

like image 759
user192082107 Avatar asked Feb 18 '13 08:02

user192082107


People also ask

What is StackedInline in Django?

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.

What is admin panel in Django?

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.

How do I change the administrator name in Django?

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.

What is the purpose of the admin site in a Django project?

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.


1 Answers

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)
like image 103
Myk Willis Avatar answered Oct 26 '22 23:10

Myk Willis