4

For my application I need to do extra operations when a model is saved via form. In practice, I need to add a value in another model if certain conditions are present in the form.

To do this I have two options, but I want to understand the pros and cons of both.

  1. Use the post_save signal
  2. Overwrite the save_model method in admin.py, since it is said in the documentation that "Overriding this method allows doing pre- or post-save operations."

I currently use the latter in this way

def save_model(self, request, obj, form, change):

    #some pre save operations....

    #this call the save model method
    super(MyModelAdmin, self).save_model(request, obj, form, change)

    #some post save operations...

and it works

But what I want to understand is:

  1. For what I must do, what are the differences between the two approaches and what is the most correct.
  2. Is the save_model method related to the use of the admin interface? What happens if I use another frontend different from Django's admin?
  3. In general, what is the difference between doing pre and post save operations overwriting save_model and using signals?
max
  • 366
  • 5
  • 18
  • The way i see it, a model object could be saved from anywhere inside your app, not just django admin. Only way to make sure something happens every time an object is saved is to use pre/post_save signals or override .save() method. After that all you have to do is watch out for bulk updates. – philoj Feb 23 '19 at 16:44

3 Answers3

5

I think you got it right. And this might help you to understand the difference.

save_model method from ModelAdmin called when you are trying to create or update something from django admin only but signals are triggered regardless of place where actions happened. Which means pre or post operations in save_model method won't work if you change model from somewhere outside of django admin but signals will work for both from outside of admin views and from your custom written code blocks.

Davit Tovmasyan
  • 3,238
  • 2
  • 20
  • 36
1

If possible i would prefer to add signals over overwriting the save_model. Signals allow certain senders to notify a set of receivers that some action has taken place. They're especially useful when many pieces of code may be interested in the same events. Also it is like event driven programming paradigm. That helps to make code organize and clean.

Shakil
  • 4,520
  • 3
  • 26
  • 36
0

Let us take a rather regular example of the process. We want to register a new user and we also have created a profile model that we want a user's profile saved only when valid User information is saved.

We can create a new User in the Django's admin and use the saved_model method in the ModelAdmin to save other parts of the form to the Profile model. This is the "normal" way of doing it. But using this way, you can only access the User and the Profile from the Django Admin panel. A "User Registration" form outside of the Django Admin would not work since the User would have to be registered to access Django Admin controls.

But using the post_save signal, a new User and Profile can be created and updated whether you use the Django Admin panel or a custom form that saves to the User and Profile models.

Therefore using signals is the most flexible but you will now have to make sure you have a way of validating the information coming from the custom form.

Afrowave
  • 911
  • 9
  • 21