覆盖特定模型的Django管理网址? [英] Override Django Admin URLs for Specific Model?

查看:105
本文介绍了覆盖特定模型的Django管理网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先有一点背景:

我有一个事件模型,具有各种 EVENT_TYPE 秒。我想将其中一个事件类型电影分解成自己的管理员。我有基本功能:代理模型继承自 Event ,命名为电影,该代理的自定义管理器模型将其过滤到仅电影事件类型,并且它是自己的ModelAdmin。

I have an Event model that has various event_types. I want to break one of those event types, 'Film', into it's own admin. I have the basic functionality in place: a proxy model inheriting from Event, named Film, a custom manager for that proxy model that filters it to only 'film' event types, and it's own ModelAdmin.

问题出在反过来。我现在需要从主要的事件管理员滤除电影。我不想修改事件模型或其默认管理器,因为影响太广泛了。所以,我尝试创建另一个代理模型 EventAdminProxy ,其唯一目的是在管理员中提供一个过滤的事件列表。然后,我使用现有的ModelAdmin注册此模型,而不是 Event

The problem is with the reverse. I now need to filter out films from the main Event admin. I don't want to alter the Event model or its default manager, because the impact would be too widespread. So, I tried creating another proxy model, EventAdminProxy, with the sole purpose of providing a filtered list of events in the admin. I then register this model, instead of Event, with the existing ModelAdmin.

这显然有效,但它有在管理员中更改URL的不幸的副作用。而不是更改列表处于/ admin / event / event /,现在位于/ admin / event / eventadminproxy /\".

This obviously works, but it has the unfortunate side-effect of altering the URLs in the admin. Instead of the changelist being at "/admin/event/event/", it's now at "/admin/event/eventadminproxy/".

我正在尝试做保存这个设置,还要保留旧的URL。我试过重载ModelAdmin的 get_urls 方法,但是从我可以看出来,你无法控制完整的URL,只有在/ app_label / model_class /part。

What I'm trying to do is keep this setup, but also keep the old URL. I've tried overloading the ModelAdmin's get_urls method, but from what I can tell, you can't control the full URL there, only what comes after the "/app_label/model_class/" part.

我以为在主要urls.py中覆盖它,但是找不到可以接受的视图来绑定。实际的视图仅适用于实例化的ModelAdmin对象,而不是类本身。

I thought about overriding it in the main urls.py, but can't figure out an acceptable view to tie into. The actual views are only available on the instantiated ModelAdmin object, not the class itself.

有什么想法来覆盖管理员中使用的URL?

Any ideas of how override the URL being used in the admin?

推荐答案

你可以覆盖验证集 - 方法,并过滤查询集,以便排除电影事件。

You could override the queryset-method of your EventModelAdmin and filter the queryset so that Film-Events get excluded.

类似于此的

class EventAdmin(admin.ModelAdmin):

    def queryset(self, request):
        qs = super(EventAdmin, self).queryset(request)
        return qs.exclude(event_type='film')

这篇关于覆盖特定模型的Django管理网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆