Django中User表上的多对多关系

时间:2012-03-28 00:55:29

标签: django recursion model m2m

我正在编写一个应用程序,我需要将数据与用户对关联起来。例如,每个用户对将具有与其相关联的兼容性分数,以及多对多关系,例如他们共有的艺术家。我对最好的方法感到困惑,似乎我会使用1)通过一对一的关系扩展用户,2)在User表上使用自我的递归关系,3)耦合使用specifying extra fields on M2M relationships,但我无法理解模型的样子。

这就是我目前正在实现的目标,我认为这不是最好的方法,因为每次查询需要两次通过数据库:

在models.py中(伪代码,假设有一个Artist类):

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    zipcode = models.CharField(max_length=16)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)         

class Score(models.Model):
    user = models.ForeignKey(User, related_name='score_first_user')
    second_user = models.ForeignKey(User, related_name='score_second_user')
    dh_score = models.DecimalField(decimal_places=2, max_digits=5)
    cre_date = models.DateTimeField(auto_now_add=True)
    upd_date = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField()

    class Meta:
        unique_together = ('user', 'second_user')   

class UserArtist(models.Model):
    user = models.ForeignKey(User, related_name='userartist_first_user')
    second_user = models.ForeignKey(User, related_name='userartist_second_user')
    artist = models.ForeignKey(Artist)
    cre_date = models.DateTimeField(auto_now_add=True)
    upd_date = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField()

然后在views.py中我使用类似(伪代码)的东西保存分数和普通艺术家:

s = Score(user=u, second_user=second_user score=dh_score)
s.save()

并使用以下内容检索它们:

u = User.objects.get(username="%s" % username)
user_scores = Score.objects.filter( Q(user=u.id) | Q(second_user=u.id) ).order_by('-dh_score')[:10]

for user_score in user_scores:
# non-relevant logic to determine who is user and who is partner
...

    partner_artists = UserArtist.objects.filter( (Q(user=u.id) & Q(second_user=partner.id))\
                                           | (Q(user=partner.id) & Q(second_user=u.id))
)

实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:4)

以下是我完成用户到用户数据配对以及与中间表建立M2M关系的方法:

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    pair = models.ManyToManyField('self', through='PairData', symmetrical=False)


    def __unicode__(self):
        return "%s's profile" % self.user

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)

class PairData(models.Model):
    first_user = models.ForeignKey(UserProfile, related_name='first_user')
    second_user = models.ForeignKey(UserProfile, related_name='second_user')
    raw_score = models.DecimalField(decimal_places=4, max_digits=9)
    dh_score = models.DecimalField(decimal_places=2, max_digits=5)
    distance = models.PositiveIntegerField()
    cre_date = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u"%s %s %f %d" % (self.first_user, self.second_user, self.dh_score, self.distance)   

class Artist(models.Model):
    pair = models.ManyToManyField(PairData)
    artist_name = models.CharField(max_length=256)

    def __unicode__(self):
        return u"%s" % self.artist_name

以下是我查询配对数据(views.py)的示例:

def matches(request, username):
    user_profile = User.objects.get(username=username).get_profile()
    pd = PairData.objects.filter( Q(first_user=user_profile) | Q(second_user=user_profile) ).order_by('-dh_score')

与每对相关的艺术家:

def user_profile(request, username):
    user_profile = User.objects.get(username=username).get_profile()
    viewers_profile = request.user.get_profile()

    pair = PairData.objects.filter( (Q(first_user=user_profile)    & Q(second_user=viewers_profile)) \
                                  | (Q(first_user=viewers_profile) & Q(second_user=user_profile)) )

    artists = Artist.objects.filter(pair=pair)

如果有更好的查询方式而不使用Q,请分享!