简体   繁体   English

在Django中取消注册第三方ModelAdmin会导致NotRegistered错误

[英]Unregistering a third-party ModelAdmin in Django causes NotRegistered error

I want to completely hide a model from a third-party module ( otherapp ) in admin site. 我想在管理站点中完全隐藏来自第三方模块( otherapp )的模型。

I put admin.site.unregister(ModelToHide) in one of my apps' admin ( myapp ), but it causes NotRegistered error on every page. 我将admin.site.unregister(ModelToHide)放在我的某个应用程序的管理员( myapp )中,但它会在每个页面上导致NotRegistered错误。 The problem is that myapp.admin is loaded before otherapp.admin . 问题是myapp.admin otherapp.admin 之前 otherapp.admin

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. 我试图改变顺序INSTALLED_APPS以便otherappmyapp之前出现,但它没有帮助,Django以相反的顺序加载它们。 I read in one of the SO comments that order of INSTALLED_APPS does not matter, as per Django book. 我在其中一篇评论中读到,根据Django的书, INSTALLED_APPS顺序并不重要。

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. 希望这有助于某人。

There are two options: 有两种选择:

  1. Put admin.site.unregister(ModelToHide) just after admin.autodiscover() in urls.py so that it runs after all models have been registered. admin.site.unregister(ModelToHide)放在urls.py中的admin.autodiscover() ,以便在所有模型注册后运行。
  2. Add an import to myapp.admin : import otherapp.admin so that it's loaded first. 添加导入到myapp.adminimport otherapp.admin以便首先加载它。

Inspired by 1 and 2 . 灵感来自12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM