0

userProfile と関係の 2 つのモデルがあります。ユーザーは相互にフォローでき、リレーションシップ モデルを通じてリレーションシップが作成されます。コードは次のとおりです。

class UserProfile(models.Model):    
    slug = models.SlugField(max_length=200)
    user = models.ForeignKey(User, unique =True)
    relationships = models.ManyToManyField('self', through='Relationship',symmetrical=False,null=True, blank=True,related_name='related_to')

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)

特定のユーザーが続く userProfile のリストを除いて、userProfile のリストを取得しようとしています。これが私のクエリです:

topUsersRecommendation = UserProfile.objects.exclude(id=profile.id,relationships__to_people__from_person = profile).extra(
   select={
    'num_group': """
     SELECT COUNT(*) FROM axiom_alto_groupuser gu1
     JOIN axiom_alto_groupuser gu2 on gu1.group_id = gu2.group_id
     WHERE gu2.user_id=axiom_alto_userprofile.id 
     AND gu1.user_id = %d """ % profile.id,
},

).order_by('-num_group')

しかし、除外は機能していないようです。お世話になりました^^

4

1 に答える 1

0

UserProfile次のように定義されたフィールドを追加できます。

followers = models.ManyToManyField(
    'self',
    blank=True,
    symmetrical=False,
    through='Relationship',
    related_name='followees'
)

IIRC では、次のようなことができます。

certain_user = UserProfile.objects.get(slug='Bob')
all_but_certain_user_followees = UserProfile.objects.all().exclude(
    follower = certain_user
)
于 2013-04-09T07:41:04.880 に答える