Django 过滤器,其中模型没有外来对象或外来字段等于

发布于 2025-01-12 09:11:47 字数 1460 浏览 2 评论 0原文

我有两个模型 CommunityUserCommunity

社区模型

class Community(models.Model):

    # Community name
    name = models.CharField(max_length=64)
    slug = models.CharField(max_length=40, blank=True)
    admins = models.ManyToManyField(
        settings.AUTH_USER_MODEL, related_name="admins", blank=True
    )
    admins = models.ManyToManyField(
        settings.AUTH_USER_MODEL, related_name="admins", blank=True
    )
    members = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name="members",
        blank=True,
    )
    ------

UserCommunityModel

class UserCommunity(models.Model):

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=CASCADE, related_name="user"
    )
    group = models.ForeignKey(
        Community, on_delete=CASCADE, related_name="group")
    role = models.CharField(max_length=15, blank=True, null=True)
    # True is in community, false is not in community
    active = models.BooleanField(null=True, blank=True)

我需要获取所有社区对象,其中用户不属于社区,并且社区应至少有 1 个成员

我尝试过使用这个

Community.objects.filter(group__user=request.user.id, group__active=False,state=location.state, group_discoverability="public").exclude(
        hofAdmins__isnull=True, admins__isnull=True, members__isnull=True)[:10]

但这会返回具有 UserCommunity 对象的社区,UserCommunity 对象是在用户加入社区时创建的。 任何帮助将不胜感激,谢谢!

I have two models Community and UserCommunity

Community Model

class Community(models.Model):

    # Community name
    name = models.CharField(max_length=64)
    slug = models.CharField(max_length=40, blank=True)
    admins = models.ManyToManyField(
        settings.AUTH_USER_MODEL, related_name="admins", blank=True
    )
    admins = models.ManyToManyField(
        settings.AUTH_USER_MODEL, related_name="admins", blank=True
    )
    members = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name="members",
        blank=True,
    )
    ------

UserCommunityModel

class UserCommunity(models.Model):

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=CASCADE, related_name="user"
    )
    group = models.ForeignKey(
        Community, on_delete=CASCADE, related_name="group")
    role = models.CharField(max_length=15, blank=True, null=True)
    # True is in community, false is not in community
    active = models.BooleanField(null=True, blank=True)

I need to get all the community objects where a user is not part of the community and the community should have at least 1 member

I've tried using this

Community.objects.filter(group__user=request.user.id, group__active=False,state=location.state, group_discoverability="public").exclude(
        hofAdmins__isnull=True, admins__isnull=True, members__isnull=True)[:10]

But this returns the communities which has a UserCommunity object, UserCommunity object is created when user joins the community.
Any help would be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2025-01-19 09:11:47

您可以使用:

Community.objects.exclude(
    group__user=request.user,
    group__active=True
).exclude(
    mebers=request.user
).exclude(
    admins=request.user
)

因此,当存在具有 active=Trueuser= 的 UserCommunity 时,这将排除 Community请求.用户

或者当社区应该至少有一名成员时:

Community.objects.filter(
    group__active=True
).exclude(
    group__user=request.user,
    group__active=True
).exclude(
    mebers=request.user
).exclude(
    admins=request.user
).distinct()

我还建议删除许多 ManyToManyField:您的 UserCommunity 已经是 用户模型和社区之间的联结表 [wiki]。例如,您可以添加字段角色来消除成员和管理员之间的歧义。通过制作多个多对多表,查询效率会降低,注释也会更加麻烦。


注意lated_name=… 参数 [Django-doc]
反向关系的名称,因此从User模型到Community
在这种情况下的模型。因此,将其命名为(通常)没有多大意义
与前向关系相同。因此,您可能需要考虑将 admins 关系重命名为 admin_communities

You can work with:

Community.objects.exclude(
    group__user=request.user,
    group__active=True
).exclude(
    mebers=request.user
).exclude(
    admins=request.user
)

This will thus exclude a Community from the moment there is a UserCommunity with active=True, and user=request.user.

or when the community should have at least one member:

Community.objects.filter(
    group__active=True
).exclude(
    group__user=request.user,
    group__active=True
).exclude(
    mebers=request.user
).exclude(
    admins=request.user
).distinct()

I would also advise to remove the many ManyToManyFields: your UserCommunity already is the junction table [wiki] between the user model and the Community. You can add a field role for example to disambiguate between members and admins. By making multiple many-to-many tables, the query will be less efficient and annotating will be more cumbersome.


Note: The related_name=… parameter [Django-doc]
is the name of the relation in reverse, so from the User model to the Community
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming the admins relation to admin_communities.

初相遇 2025-01-19 09:11:47
Community.objects.filter(~Q(members__in=[request.user.id]), ~Q(hofAdmins__in=[request.user.id]), ~Q(admins__in=[request.user.id]), state=location.state, group_discoverability="public").exclude(         hofAdmins__isnull=True, admins__isnull=True, members__isnull=True)[:10]
Community.objects.filter(~Q(members__in=[request.user.id]), ~Q(hofAdmins__in=[request.user.id]), ~Q(admins__in=[request.user.id]), state=location.state, group_discoverability="public").exclude(         hofAdmins__isnull=True, admins__isnull=True, members__isnull=True)[:10]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文