2011-09-21 02:30:06 +08:00
|
|
|
from functools import update_wrapper
|
|
|
|
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.db import models
|
2012-12-23 03:00:08 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Action(models.Model):
|
|
|
|
name = models.CharField(max_length=50, primary_key=True)
|
|
|
|
description = models.CharField(max_length=70)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2011-09-21 02:30:06 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class ActionAdmin(admin.ModelAdmin):
|
|
|
|
"""
|
|
|
|
A ModelAdmin for the Action model that changes the URL of the add_view
|
|
|
|
to '<app name>/<model name>/!add/'
|
|
|
|
The Action model has a CharField PK.
|
|
|
|
"""
|
|
|
|
|
|
|
|
list_display = ("name", "description")
|
|
|
|
|
|
|
|
def remove_url(self, name):
|
|
|
|
"""
|
|
|
|
Remove all entries named 'name' from the ModelAdmin instance URL
|
|
|
|
patterns list
|
|
|
|
"""
|
2017-01-21 21:13:44 +08:00
|
|
|
return [url for url in super().get_urls() if url.name != name]
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
def get_urls(self):
|
|
|
|
# Add the URL of our custom 'add_view' view to the front of the URLs
|
|
|
|
# list. Remove the existing one(s) first
|
2018-12-08 06:52:28 +08:00
|
|
|
from django.urls import re_path
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
def wrap(view):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return self.admin_site.admin_view(view)(*args, **kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-09-21 02:30:06 +08:00
|
|
|
return update_wrapper(wrapper, view)
|
|
|
|
|
2013-02-05 17:16:07 +08:00
|
|
|
info = self.model._meta.app_label, self.model._meta.model_name
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
view_name = "%s_%s_add" % info
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
return [
|
2018-12-08 06:52:28 +08:00
|
|
|
re_path("^!add/$", wrap(self.add_view), name=view_name),
|
2014-04-02 08:46:34 +08:00
|
|
|
] + self.remove_url(view_name)
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
|
2012-12-23 03:00:08 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=20)
|
2012-10-19 07:58:52 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-12-23 03:00:08 +08:00
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
2012-12-31 13:33:21 +08:00
|
|
|
def response_post_save_add(self, request, obj):
|
2012-12-23 03:00:08 +08:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse("admin:admin_custom_urls_person_history", args=[obj.pk])
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2012-10-19 07:58:52 +08:00
|
|
|
|
2012-12-31 13:33:21 +08:00
|
|
|
def response_post_save_change(self, request, obj):
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse("admin:admin_custom_urls_person_delete", args=[obj.pk])
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2012-12-31 13:33:21 +08:00
|
|
|
|
2012-10-19 07:58:52 +08:00
|
|
|
|
2012-12-23 03:00:08 +08:00
|
|
|
class Car(models.Model):
|
|
|
|
name = models.CharField(max_length=20)
|
2012-10-19 07:58:52 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-12-23 03:00:08 +08:00
|
|
|
class CarAdmin(admin.ModelAdmin):
|
|
|
|
def response_add(self, request, obj, post_url_continue=None):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().response_add(
|
|
|
|
request,
|
|
|
|
obj,
|
|
|
|
post_url_continue=reverse(
|
|
|
|
"admin:admin_custom_urls_car_history", args=[obj.pk]
|
|
|
|
),
|
|
|
|
)
|
2012-10-19 07:58:52 +08:00
|
|
|
|
|
|
|
|
2015-03-31 00:33:26 +08:00
|
|
|
site = admin.AdminSite(name="admin_custom_urls")
|
|
|
|
site.register(Action, ActionAdmin)
|
|
|
|
site.register(Person, PersonAdmin)
|
|
|
|
site.register(Car, CarAdmin)
|