How do you unregister a model in wagtail modeladmin?

∥☆過路亽.° 提交于 2019-12-20 01:41:50

问题


I need to do the equivalent of... 'admin.site.unregister(Value)' but for a model registered with wagtailmodeladmin using 'modeladmin_register(Value)' in wagtail_hooks.py. How do you do that?


回答1:


I know this is an old question, but the short answer is "There is no unregister equivalent".

In standard Django, all the models you see in Django's admin area have been registered in a similar fashion, so unregister makes sense there. In Wagtail, the admin area is completely custom, and 'modeladmin' isn't central to the admin architecture like Django's similar solution is. The various apps within Wagtail do not use import/use it to register their own models. Therefore, there is no way to 'unregister' any of those apps using modeladmin. The 'modeladmin' app is just a utility thing to help you add interfaces for additional models without having to understand all of the various hooks provided by wagtail to do such things.

So, with 'modeladmin' only being used to register custom models, the need for an 'unregister' method is greatly reduced, because in most cases, you'll be in control of what models you are registering via the modeladmin_register method, and so you should be able to just 'not register' those.




回答2:


No built-in way to do it, but if you wanted to add your own way:

# helpers.py
from wagtail import hooks    

def replace_hook(hook_name, original_fn):
    hooks._hooks[hook_name].remove((original_fn, 0))
    def inner(fn):
        hooks.register('register_page_listing_buttons', fn)
        return fn
    return inner

Let's say we wanted to remove all buttons from the listing view except the "add child page":

# wagtail_hooks.py
import helpers

@replace_hook('register_page_listing_buttons', page_listing_buttons)
def remove_redundant_buttons(page, page_perms, is_parent=False):
    buttons = page_listing_buttons(page, page_perms, is_parent)
    if isinstance(page, models.BasePage):
        return buttons
    else:
        # for non-subclasses-of-BasePage allow only adding children
        allowed_urls = ['add_subpage']
        return [
            item for item in buttons
            if item.url and resolve(item.url).url_name in allowed_urls
        ]

Result:




回答3:


I haven't found such ability in wagtail, but for my case, it was enough to exclude a model from the main menu, so I did next:

@hooks.register('construct_main_menu')
def hide_longlaw_order(request, menu_items):
    menu_items[:] = [item for item in menu_items if 'longclaworders' not in item.url]


来源:https://stackoverflow.com/questions/38793852/how-do-you-unregister-a-model-in-wagtail-modeladmin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!