Django 过滤器,其中模型没有外来对象或外来字段等于
我有两个模型 Community
和 UserCommunity
社区模型
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用:
因此,当存在具有
active=True
和user= 的
。UserCommunity
时,这将排除Community
请求.用户或者当社区应该至少有一名成员时:
我还建议删除许多
ManyToManyField
:您的UserCommunity
已经是 用户模型和社区
之间的联结表 [wiki]。例如,您可以添加字段角色
来消除成员和管理员之间的歧义。通过制作多个多对多表,查询效率会降低,注释也会更加麻烦。You can work with:
This will thus exclude a
Community
from the moment there is aUserCommunity
withactive=True
, anduser=request.user
.or when the community should have at least one member:
I would also advise to remove the many
ManyToManyField
s: yourUserCommunity
already is the junction table [wiki] between the user model and theCommunity
. You can add a fieldrole
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.