【发布时间】:2014-01-05 22:52:13
【问题描述】:
型号:
class Customer(models.Model):
name = models.CharField(max_length=200)
time_categoty = models.IntegerField()
importance = models.IntegerField()
class Interaction(models.Model):
i_date = models.DateTimeField()
class Interaction_customer(models.Model):
interaction = models.ForeignKey(Interaction)
customer = models.ForeignKey(Customer)
假设:
c = Customer.objects.get(pk=1)
x = Interaction.objects.filter(interaction_person__person=c).latest('i_date').i_date
(即给定客户的最新互动)
需要:所有客户的列表,按条件排序: time_category/(datetime.now() - x) * c.importance
请不要在 djangoproject.com 上给我一个“extra”的链接,我已经仔细阅读了它,但真的不知道如何在我的情况下实现它。 (案子很难,一个星期没人能帮忙,需要django-Jedi) 任何建设性的想法将不胜感激。 谢谢! //埃德
【问题讨论】:
-
对不起,interaction_person__person == interaction_customer__customer
-
1.最好将计算保存在某处,以便于排序(没有其他明确的方法可以做到这一点而不放弃一些效率) 2. 你的模型看起来像ManyToMany with a 'through'。我不知道这是否是您所说的“额外”,如果 Interaction_Customer 不添加自己的任何其他字段,我完全不明白您需要什么。最好直接链接它们
-
我已经删除了所有与案例无关的字段,但真正的基础只需要这样的结构。 //埃德
-
是否反对将 ManyToMany 与 'through' 结合使用?看起来像您正在寻找的设计
-
是的,没错。这就是“通过”的目的。有关详细实施,请参阅我的答案。它只是让它们之间更容易直接连接
标签: django django-models django-queryset django-orm