Django: How to move save button for model admin?

I have a model with a status field. When a user uses the Admin application to change an instance, how do I click on the "Save" button, click so that I can update the "status" to a value that depends on the registered username of the user?

+5
source share
4 answers

Override your modeladmin save_model-method:

class ModelAdmin(admin.ModelAdmin):       
    def save_model(self, request, obj, form, change):
        user = request.user 
        instance = form.save(commit=False)
        if not change:    # new object
            instance.status = ....
        else:             # updated old object
            instance.status = ...
        instance.save()
        form.save_m2m()
        return instance
+13
source

Use a signal pre_save. Of course, it will be called during each operation of saving the instance, not only from the administrator, but also in relation to your situation.

+6

ModelAdmin.save_model () provides only what I need

+4
source
form.cleaned_data.get('categories')

This gets the value directly in ModelAdmin.save_model

0
source

Source: https://habr.com/ru/post/1763338/


All Articles