0

我正在学习 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)

编辑:

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)
4

4 回答 4

0

我一直在寻找一种方法或某种方式来做到这一点,但我一无所获。但有办法做到这一点。您可以简单地使用 for 循环遍历所有对象并删除它们具有特殊属性值的所有对象。这里有一个示例代码:

all_objects = className.objects.all()
for obj in all_objects:
    if obj.some_attribute == "some_value":
        all_objects.remove(obj)
于 2013-01-24T09:15:34.027 回答
0

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

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

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

于 2012-08-17T20:16:21.530 回答
-1

我没有对此进行测试,但它认为它应该做你想要的。

def get_non_followers(self):
    return self.related_to.exclude(
    from_people__to_person=self)  
于 2012-08-17T18:04:05.120 回答
-1
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是您要查找的对象usernon_followers

于 2012-08-17T17:39:17.400 回答