【问题标题】:Wagtail: get parent page groups that have specific rights (edit, delete)Wagtail:获取具有特定权限(编辑、删除)的父页面组
【发布时间】:2018-01-30 03:37:02
【问题描述】:

我在 wagtail 中有一个 categorypage -> articlepage 层次结构。文章页面有一个作者字段,当前显示系统中的所有用户。我想根据父类别页面的组过滤文章的作者。

models.py

from django.contrib.auth.models import Group

class CategoryPage(Page):  # query service, api
    blurb = models.CharField(max_length=300, blank=False)
    content_panels = Page.content_panels + [
        FieldPanel('blurb', classname="full")
    ]
    subpage_types = ['cms.ArticlePage']


class ArticlePage(Page):  # how to do X with query service
    ...
    author = models.ForeignKey(User, on_delete=models.PROTECT, default=1,
                           # limit_choices_to=get_article_editors,
                           help_text="The page author (you may plan to hand off this page for someone else to write).")

def get_article_editors():
    # get article category
    # get group for category
    g = Group.objects.get(name='??')
    return {'groups__in': [g, ]}

This question (limit_choices_to) 差不多就是我所追求的,但是在文章本身创建之前不知道如何检索父页面的分组?

This question 似乎可以在创建时访问父页面,但我仍然不确定如何找到可以编辑父页面的组。

【问题讨论】:

    标签: django wagtail


    【解决方案1】:

    不幸的是,我不知道limit_choices_to 函数接收对父对象的引用的方法。您的第二个链接在正确的轨道上,我们需要为页面提供我们自己的基本表单并调整 author 字段的查询集。

    from django.contrib.auth.models import Group
    from wagtail.wagtailadmin.forms import WagtailAdminPageForm
    from wagtail.wagtailcore.models import Page
    
    
    class ArticlePageForm(WagtailAdminPageForm):
        def __init__(self, data=None, files=None, parent_page=None, *args, **kwargs):
            super().__init__(data, files, parent_page, *args, **kwargs)
    
            # Get the parent category page if `instance` is present, fallback to `parent_page` otherwise.
            # We're trying to get the parent category page from the `instance` first
            # because `parent_page` is only set when the page is new (haven't been saved before).
            instance = kwargs.get('instance')
            category_page = instance.get_parent() if instance and instance.pk else parent_page
            if not category_page:
                return  # Do not continue if we failed to find the parent category page.
    
            # Get the groups which have permissions on the parent category page.
            groups = Group.objects.filter(page_permissions__page_id=category_page.pk).distinct()
            if not groups:
                return  # Do not continue if we failed to find any groups.
    
            # Filter the queryset of the `author` field.
            self.fields['author'].queryset = self.fields['author'].queryset.filter(groups__in=groups)
    
    
    class ArticlePage(Page):
        base_form_class = ArticlePageForm
    

    关于我们查询组的方式的简要说明: 当您在 Wagtail 中设置页面权限时,您实际上创建了一个 GroupPagePermission,它有两个主要属性,grouppagegroupGroupPagePermission 的外键的related_name 被定义为page_permissions 并且每次创建ForeignKey 就像page 一样,它实际上创建了一个名为page_id 的字段。因此我们可以按照关系,通过page_permissions__page_id和父分类页面的ID过滤分组。

    【讨论】:

    • 感谢 Loic,我已经更新了我的问题以包含完整的 Category 模型。从父对象中检索组实际上是我坚持的一点:AttributeError: 'Page' object has no attribute 'group_name'
    • 我在这个定义中没有看到组名。您打算如何过滤作者?是否使用 Django 组?类别页面会与组有相同的 slug 吗?
    • 该组是 django 组。在 wagtail 中,我设置了组权限,以便特定组只能访问特定的类别页面。我想在创建新页面时访问该组,以便我可以据此过滤作者(用户)。
    • 对不起,我忘了说我已经更新了答案。
    猜你喜欢
    • 2023-03-03
    • 2016-04-01
    • 2018-12-31
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多