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

これは特に魅力的ではありませんが、正しい結果が得られます (テスト済み):

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 に答える
0

私はそれを行う方法や方法を1時間ほど探していましたが、何も見つかりませんでした. しかし、それを行う方法があります。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 に答える
-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 に答える
-1

私はこれをテストしていませんが、あなたが望むことをするべきだと思います。

def get_non_followers(self):
    return self.related_to.exclude(
    from_people__to_person=self)  
于 2012-08-17T18:04:05.120 に答える