Further customization of Django admin (add new obj

2019-08-04 06:26发布

I've done a few configurations (inlines, filters, order_by...) to the Django admin interface but I would like to change the behavior of the save button when you add a new item. Take a look at my interface (It is in French, but it should be easy to understand prix:price - enregistrer:save)

Django admin interface

I have three price field, one for the current item price and two other for previous prices. What I want to do is when the current price is changed and saved, the data from price goes to price2 and price2 to price3 automatically. Like that, I always see what the older prices were.

And maybe one more thing : is it possible that when save(enregistrer) is clicked, I can return to a particular page, and not to the list of all objects?

Thanks for helping!

2条回答
时光不老,我们不散
2楼-- · 2019-08-04 06:46

There are several ways to accomplish your requirement. The two most obvious are:

  1. Override the save method of your model (refer to the Django docs).
  2. Override the save_model method of your ModelAdmin.

I would go for 1. because this looks like logic that you don't want to tie to the admin explicitly, but it depends on the app you are building.

Regarding the redirect, you might want to have a look at Redirect on admin Save.

查看更多
放荡不羁爱自由
3楼-- · 2019-08-04 06:50

You can override admin save method like that:

class MyModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        # custom stuff here
        obj.save()

and this question may help you with redirect after save: Redirect on admin Save

查看更多
登录 后发表回答