Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create menus programmatically #410

Open
kiwiheretic opened this issue Feb 2, 2022 · 1 comment
Open

Create menus programmatically #410

kiwiheretic opened this issue Feb 2, 2022 · 1 comment

Comments

@kiwiheretic
Copy link

How would I create wagtailmenus menu programatically (that is not through the wagtail admin). If we assume that all the necessary pages exist in the database then what would this look like?

For instance I imagine we would create the main menu record and then add all the main menu item records to it somehow and pass in the relevant pages to the constructor. However even though I can find the model for menu items I haven't managed to find the model for the main top record menu itself. Any idea on how I would go about this?

@arrys
Copy link
Contributor

arrys commented Apr 28, 2022

The following snippet should add all level 1 pages for all sites to their main menu.

from wagtail.core.models import Site
from wagtailmenus.models import MainMenu

for site in Site.objects.all():
    menu = MainMenu.get_for_site(site)
    if not menu.get_menu_items_manager().exists():
        menu.add_menu_items_for_pages(
            site.root_page.get_descendants(inclusive=True).filter(depth__lte=site.root_page.depth + 1)
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants