在Django中注销第三方ModelAdmin会导致NotRegistered错误 [英] Unregistering a third-party ModelAdmin in Django causes NotRegistered error
问题描述
我想从管理站点中的第三方模块( otherapp
)完全隐藏模型。
I want to completely hide a model from a third-party module (otherapp
) in admin site.
我将 admin.site.unregister(ModelToHide)
放在我的一个应用程序管理员( myapp
),但会在每个页面上导致 NotRegistered
错误。问题是之前 otherapp.admin
加载了 myapp.admin
。
I put admin.site.unregister(ModelToHide)
in one of my apps' admin (myapp
), but it causes NotRegistered
error on every page. The problem is that myapp.admin
is loaded before otherapp.admin
.
我试图更改订单 INSTALLED_APPS
,以便在 otherapp
之前出现 myapp
,但这没有帮助,Django以相反的顺序加载它们。根据Django的书,我读了一条SO评论说, INSTALLED_APPS
的顺序无关紧要。
I tried to change the order INSTALLED_APPS
so that otherapp
comes before myapp
, but it did not help, Django loads them in reverse order. I read in one of the SO comments that order of INSTALLED_APPS
does not matter, as per Django book.
实际上,我提出这个问题只是为了分享我对它的工作方式,因为花了一些时间来找出解决方案。希望这对某人有帮助。
Actually, I created this question just to share the way I made it work for me, because it took some time to figure out the solution. Hope this helps someone.
推荐答案
有两种选择:
- 在
admin.autodiscover()
之后放置admin.site.unregister(ModelToHide)
urls.py,以便在所有模型都注册后运行。 - 将导入内容添加到
myapp.admin
:import otherapp.admin
使其首先被加载。
- Put
admin.site.unregister(ModelToHide)
just afteradmin.autodiscover()
in urls.py so that it runs after all models have been registered. - Add an import to
myapp.admin
:import otherapp.admin
so that it's loaded first.
这篇关于在Django中注销第三方ModelAdmin会导致NotRegistered错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!