Home > Software design >  django - different save methods for user actions and admin action
django - different save methods for user actions and admin action

Time:12-01

I have some items connected to users.

When every item is added, timestamp is created through inheritance of BaseModel on auto_now field.

By mistake when i added new field and populated i updated timestamps.

I resolved timestamps with some custom migrations and copy data methods in django.

What i wonder - is there possibility to override save method on admin to do only update_fields (so in other words with that i would not update update_at timestamp), while on user actions i want to retain original django save method which would update timestamp.

So basically is it possible to have two different save methods?

I know that i can override save method - but i don't know if i can have two save methods at the same time.

CodePudding user response:

ModelAdmin.save_model() might deliver that for you. Check out the docs. Within your admin file, override the save_model function

class ObjectAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save(update_fields = ['fields', 'to', 'save'])

From looking at the django github, the super of save_model doesn't do much more than call obj.save(), so I don't think you need or want to call super() in this case.

  • Related