[英]Django: Order a model by a many-to-many field
I am writing a Django application that has a model for People, and I have hit a snag. 我正在编写一个Django应用程序,它有一个People模型,我遇到了障碍。 I am assigning Role objects to people using a Many-To-Many relationship - where Roles have a name and a weight.
我正在为使用多对多关系的人分配角色对象 - 其中角色具有名称和权重。 I wish to order my list of people by their heaviest role's weight.
我希望按照他们最重要的角色来命令我的人员名单。 If I do People.objects.order_by('-roles__weight'), then I get duplicates when people have multiple roles assigned to them.
如果我做People.objects.order_by(' - roles__weight'),那么当人们分配了多个角色时,我会得到重复。
My initial idea was to add a denormalized field called heaviest-role-weight - and sort by that. 我最初的想法是添加一个称为最重角色权重的非规范化字段 - 并按此排序。 This could then be updated every time a new role was added or removed from a user.
每次添加或从用户删除新角色时,都可以更新此信息。 However, it turns out that there is no way to perform a custom action every time a ManyToManyField is updated in Django ( yet , anyway).
然而,事实证明,有没有办法来执行自定义操作每次ManyToManyField是在Django(更新时间还没有 ,反正)。
So, I thought I could then go completely overboard and write a custom field, descriptor and manager to handle this - but that seems extremely difficult when the ManyRelatedManager is created dynamically for a ManyToManyField. 所以,我认为我可以完全超越并编写一个自定义字段,描述符和管理器来处理这个问题 - 但是当为ManyToManyField动态创建ManyRelatedManager时,这似乎非常困难。
I have been trying to come up with some clever SQL that could do this for me - I'm sure it's possible with a subquery (or a few), but I'd be worried about it not being compatible will all the database backends Django supports. 我一直试图想出一些聪明的SQL,可以为我做这个 - 我敢肯定它可能有一个子查询(或几个),但我担心它不兼容将所有的数据库后端Django支持。
Has anyone done this before - or have any ideas how it could be achieved? 有没有人之前做过这个 - 或者有任何想法如何实现它?
Django 1.1 (currently beta) adds aggregation support. Django 1.1(目前是测试版)增加了聚合支持。 Your query can be done with something like:
您的查询可以通过以下方式完成:
from django.db.models import Max
People.objects.annotate(max_weight=Max('roles__weight')).order_by('-max_weight')
This sorts people by their heaviest roles, without returning duplicates. 这会按照最重要的角色对人进行排序,而不会返回重复项。
The generated query is: 生成的查询是:
SELECT people.id, people.name, MAX(role.weight) AS max_weight
FROM people LEFT OUTER JOIN people_roles ON (people.id = people_roles.people_id)
LEFT OUTER JOIN role ON (people_roles.role_id = role.id)
GROUP BY people.id, people.name
ORDER BY max_weight DESC
Here's a way to do it without an annotation: 这是一种没有注释的方法:
class Role(models.Model):
pass
class PersonRole(models.Model):
weight = models.IntegerField()
person = models.ForeignKey('Person')
role = models.ForeignKey(Role)
class Meta:
# if you have an inline configured in the admin, this will
# make the roles order properly
ordering = ['weight']
class Person(models.Model):
roles = models.ManyToManyField('Role', through='PersonRole')
def ordered_roles(self):
"Return a properly ordered set of roles"
return self.roles.all().order_by('personrole__weight')
This lets you say something like: 这可以让你说:
>>> person = Person.objects.get(id=1)
>>> roles = person.ordered_roles()
Something like this in SQL: SQL中的这样的东西:
select p.*, max (r.Weight) as HeaviestWeight
from persons p
inner join RolePersons rp on p.id = rp.PersonID
innerjoin Roles r on rp.RoleID = r.id
group by p.*
order by HeaviestWeight desc
Note: group by p.* may be disallowed by your dialect of SQL. 注意:您的SQL方言可能不允许使用p。*分组。 If so, just list all the columns in table p that you intend to use in the select clause.
如果是这样,只需列出要在select子句中使用的表p中的所有列。
Note: if you just group by p.ID, you won't be able to call for the other columns in p in your select clause. 注意:如果您只是按p.ID分组,则无法在select子句中调用p中的其他列。
I don't know how this interacts with Django. 我不知道这是如何与Django交互的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.