如何将子模型中的最新记录包含到查询中
我有定期接受评估的交易员。评估对交易者来说有一个外键。我想列出所有交易者及其当前的评估状态。这可以通过添加到 Trader 模型中的自定义属性来实现。但是,我希望能够根据最新评估的状态筛选结果(例如,所有仍处于开放状态的当前评估)。但是,正如之前所示(查询中的自定义属性),您可以' t 在查询中包含自定义模型属性。
我会在 Trader 模型中创建一个外键,记录最新评估的 id,但是,这是不可能的,因为这样模型将相互引用,然后在创建类时会遇到循环排序问题,即Trader引用Evaluation,但Evaluation尚未声明,或者将Evaluation放在第一位,但Trader尚未声明。
到目前为止,我假设我将使用 Trader 作为我的基本查询,并查找评估。我认为解决该问题的另一种方法是根据评估创建查询,然后查找交易者。然而,这意味着那些尚未评估的交易者永远不会出现在结果中(除非我确保交易者始终至少有一个评估记录)。或者我想我可以求助于使用原始 SQL。
任何有关如何解决此问题的建议将不胜感激。
以下是型号:
Class Trader(models.Model):
territory = models.ForeignKey(Territory)
organisation_name = models.CharField(max_length=30)
contact_title = models.CharField(choices=TITLE_CHOICES, max_length=4)
contact_forename = models.CharField(max_length=30, blank=True)
contact_lastname = models.CharField(max_length=30)
...
def _get_current_evaluation(self):
return Evaluation.objects.filter(trader=self).latest('open_date')
current_evaluation = property(_get_current_evaluation)
Class Evaluation(models.Model):
trader = models.ForeignKey(Trader)
open_date = models.DateField("Open Date", null=True, blank=True)
rated_date = models.DateField("Rated Date", null=True, blank=True)
closed_date = models.DateField("Closed Date", null=True, blank=True)
status = models.ForeignKey(RatingStatus, null=True, blank=True)
...
I have traders that are evaluated on a regular basis. The evaluations have a foreign key to the trader. I want to list all the traders along with their current evaluation status. This is possible using a custom property added to the Trader model. However, I want to be able to filter the results on the status of the latest evaluation (for example, all those current evaluations that are still open). However, as has been shown before (Custom properties in a query), you can't include a custom model property in a query.
I would have created a foreign key in the Trader model, that records that id of the most current Evaluation, however, that's not possible, because then the models will reference each other, and then you get into an circular ordering problem when creating the classes, ie Trader refers to Evaluation, but Evaluation hasn't been declared yet, or putting Evaluation first, yet Trader isn't declared.
So far I assumed that I would use Trader as my base query, and lookup the Evaluations. I thought an alternative to get around the problem would be to create a query based on Evaluation, and then lookup Traders instead. However, this means that those Traders who have not been evaluated yet, would never appear in results (unless I made sure that Traders always have at least one Evaluation record). Or I suppose I could resort to using raw SQL.
Any suggestions on how to go about solving this would be greatly appreciated.
Here are the models:
Class Trader(models.Model):
territory = models.ForeignKey(Territory)
organisation_name = models.CharField(max_length=30)
contact_title = models.CharField(choices=TITLE_CHOICES, max_length=4)
contact_forename = models.CharField(max_length=30, blank=True)
contact_lastname = models.CharField(max_length=30)
...
def _get_current_evaluation(self):
return Evaluation.objects.filter(trader=self).latest('open_date')
current_evaluation = property(_get_current_evaluation)
Class Evaluation(models.Model):
trader = models.ForeignKey(Trader)
open_date = models.DateField("Open Date", null=True, blank=True)
rated_date = models.DateField("Rated Date", null=True, blank=True)
closed_date = models.DateField("Closed Date", null=True, blank=True)
status = models.ForeignKey(RatingStatus, null=True, blank=True)
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://docs.djangoproject.com/en/dev/ref/模型/查询集/#latest
http://docs.djangoproject.com/en/dev/参考/模型/querysets/#order-by
如果你按照evaluation_status=open进行过滤,无论如何你都不会得到还没有评估过的交易者。
所以:
这并不是真正必要的,但仅供你参考:
http://docs.djangoproject.com/en/dev /ref/models/fields/#foreignkey
http://docs.djangoproject.com/en/dev/ref/models/querysets/#latest
http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by
If you were to filter by evaluation_status=open, you wouldn't get the traders that haven't been evaluated yet anyways.
so:
This isn't really necessary but for your reference:
http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey