Tho*_*mel 14 django django-models
我想通过相关字段对联系人的QuerySet进行排序.但是我不知道怎么做.我试过这样,但它不起作用.
foundContacts.order_by("classification.kam")
Run Code Online (Sandbox Code Playgroud)
实际上在模板中我可以通过contact.classification.kam访问联系人的kam值,因为它是OneToOne关系.
(简化)模型如下所示:
class Classification(models.Model):
kam = models.ForeignKey(User)
contact = models.OneToOneField(Contact)
class Contact(models.Model):
title = models.ForeignKey(Title, blank=True, null=True)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)
Mar*_*vin 29
它应该是:
foundContacts.order_by("classification__kam")
Run Code Online (Sandbox Code Playgroud)
这是关于Django文档的链接,用于创建跨越关系的查询:http: //docs.djangoproject.com/en/1.1/topics/db/queries/#lookups-that-span-relationships
您还可以在order_by参考资料中看到一些示例:https:
//docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.order_by
| 归档时间: |
|
| 查看次数: |
16190 次 |
| 最近记录: |