在django中查找没有关系的对象

xmakbtuz  于 2023-02-05  发布在  Go
关注(0)|答案(5)|浏览(116)

我正在学习Django,想要检索所有与我正在查看的对象有关系的对象。
这个想法是一个简单的Twitter模仿者。
我正在尝试弄清楚如何实现get_non_followers

from django.db import models

RELATIONSHIP_FOLLOWING = 1
RELATIONSHIP_BLOCKED = 2
RELATIONSHIP_STATUSES = (
                         (RELATIONSHIP_FOLLOWING, 'Following'),
                         (RELATIONSHIP_BLOCKED, 'Blocked'),
)


class UserProfile(models.Model):
    name = models.CharField(max_length=200)
    website = models.CharField(max_length=200)
    email = models.EmailField()
    relationships = models.ManyToManyField('self', through='Relationship', 
                                           symmetrical=False, 
                                           related_name='related_to')
    def __unicode__ (self):
        return self.name
    
    def add_relationship(self, person, status):
        relationship, created = Relationship.objects.get_or_create(
            from_person=self,
            to_person=person,
            status=status)
        return relationship
    
    def remove_relationship(self, person, status):
        Relationship.objects.filter(
            from_person=self, 
            to_person=person,
            status=status).delete()
        return
       

    def get_relationships(self, status):
        return self.relationships.filter(
        to_people__status=status, 
        to_people__from_person=self)
        
    
    def get_related_to(self, status):
        return self.related_to.filter(
            from_people__status=status, 
            from_people__to_person=self)
    
    def get_following(self):
        return self.get_relationships(RELATIONSHIP_FOLLOWING)
    
    def get_followers(self):
        return self.get_related_to(RELATIONSHIP_FOLLOWING)

    def get_non_followers(self):
        # How to do this?
        return

class Relationship(models.Model):
    from_person = models.ForeignKey(UserProfile, related_name='from_people')
    to_person = models.ForeignKey(UserProfile, related_name='to_people')
    status = models.IntegerField(choices=RELATIONSHIP_STATUSES)
kyxcudwk

kyxcudwk1#

这不是特别迷人,但它给出了正确的结果(刚刚测试过):

def get_non_followers(self):
    UserProfile.objects.exclude(to_people=self,
        to_people__status=RELATIONSHIP_FOLLOWING).exclude(id=self.id)

简而言之,使用exclude()过滤掉当前用户之后的所有UserProfiles,这将留下用户本身(可能不应该包括在内)和所有不关注他们的用户。

okxuctiv

okxuctiv2#

我一直在寻找一个方法或某种方式来做到这一点,像一个小时,但我什么也没找到.但有一种方法可以做到这一点.你可以简单地使用一个for循环迭代通过所有对象,只是删除所有的对象,他们有一个特殊的属性值.这里有一个示例代码:

all_objects = className.objects.all()
for obj in all_objects:
    if obj.some_attribute == "some_value":
        all_objects.remove(obj)
gfttwv5a

gfttwv5a3#

get_non_followers实现的解决方案:

def get_non_following(self):
    return UserProfile.objects.exclude(to_person__from_person=self, to_person__status=RELATIONSHIP_FOLLOWING).exclude(id=self.id)

这个答案是由CC BY-SA 3.0下的OP Avi Meir发布的问题在django中查找没有关系的对象的edit

xmq68pz9

xmq68pz94#

current_userprofile = current_user.get_profile()
rest_of_users = Set(UserProfile.objects.filter(user != current_userprofile))
follow_relationships = current_userprofile.relationships.filter(from_person=current_user)
followers = Set();
for follow in follow_relationships:
    followers.add(follow.to_person)

non_followeres = rest_of_users.difference(followers)

这里non_followers是您想要的用户配置文件列表。current_user是您要查找的non_followersuser

z31licg0

z31licg05#

我还没有测试过,但我想它应该能满足你的要求。

def get_non_followers(self):
    return self.related_to.exclude(
    from_people__to_person=self)

相关问题