理想情况下,我想创建一个注册多个字段的注释。这是代码:
#the model
class Referral(models.Model):
product = models.ForeignKey(Product)
region = models.ForeignKey(Region)
owner = models.ForeignKey(User, related_name='owner')
receiver = models.ForeignKey(User, related_name='receiver')
company_name = models.CharField(max_length=255)
note = models.TextField(max_length=5000, blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
#I know how to get either owner or recipient, but how to annotate owner + recipient combined?
users = User.objects.annotate(num_referrals=Count('owner')).order_by('-num_referrals')[:3]
有优雅的方法吗?
答案 0 :(得分:0)
试试这个
users = User.objects \
.extra(select={'total': 'Count(owner)+Count(receiver)'}) \
.order_by('-total')[:3]