如何将子模型中的最新记录包含到查询中

发布于 2024-10-28 04:54:29 字数 1482 浏览 3 评论 0原文

我有定期接受评估的交易员。评估对交易者来说有一个外键。我想列出所有交易者及其当前的评估状态。这可以通过添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2024-11-04 04:54:29

http://docs.djangoproject.com/en/dev/ref/模型/查询集/#latest
http://docs.djangoproject.com/en/dev/参考/模型/querysets/#order-by

“我想列出所有交易者
以及他们当前的评估状态。”

traders = Trader.objects.all()

{% for trader in traders %}
    {{ trader }} : {{ trader.evaluation_set.latest.status|default:"Not Evaluated" }}
{% endfor %}

“但是,我希望能够过滤
的状态结果
最新评估(例如,所有
目前的评估是
仍然开放)。”

“我想到了另一种出行方式
问题是创建一个查询
基于Evaluation,然后查找
而是交易员。然而,这意味着
那些没有参加过的交易者
尚未评估,永远不会出现在
结果(除非我确定
交易者总是至少拥有一个
评价记录)。”

如果你按照evaluation_status=open进行过滤,无论如何你都不会得到还没有评估过的交易者。

所以:

status = RatingStatus.get(status='open')
traders_with_open_evals = Traders.objects.filter(evaluation__status=status).distinct()

这并不是真正必要的,但仅供你参考:

我会创建一个外键
记录该 id 的 Trader 模型
最新的评估,
然而,这是不可能的,因为
那么模型将引用每个
其他,然后你就进入了
循环排序问题
创建类,即 Trader 引用
到评估,但评估还没有
尚未宣布,或将
评估第一,但交易者不是
声明。

http://docs.djangoproject.com/en/dev /ref/models/fields/#foreignkey

如果您需要建立关系
在尚未建立的模型上
定义后,您可以使用
模型,而不是模型对象
本身:

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...

class Manufacturer(models.Model):
    # ...

引用另一个中定义的模型
应用程序,您可以明确
指定一个完整的模型
应用程序标签。例如,如果
上面的制造商型号定义在
另一个称为生产的应用程序,
你需要使用:

class Car(models.Model):
    manufacturer = models.ForeignKey('production.Manufacturer')

这种参考很有用
解决循环导入时
两个应用程序之间的依赖关系。

http://docs.djangoproject.com/en/dev/ref/models/querysets/#latest
http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by

"I want to list all the traders along
with their current evaluation status."

traders = Trader.objects.all()

{% for trader in traders %}
    {{ trader }} : {{ trader.evaluation_set.latest.status|default:"Not Evaluated" }}
{% endfor %}

"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)."

"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)."

If you were to filter by evaluation_status=open, you wouldn't get the traders that haven't been evaluated yet anyways.

so:

status = RatingStatus.get(status='open')
traders_with_open_evals = Traders.objects.filter(evaluation__status=status).distinct()

This isn't really necessary but for your reference:

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.

http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

If you need to create a relationship
on a model that has not yet been
defined, you can use the name of the
model, rather than the model object
itself:

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...

class Manufacturer(models.Model):
    # ...

To refer to models defined in another
application, you can explicitly
specify a model with the full
application label. For example, if the
Manufacturer model above is defined in
another application called production,
you'd need to use:

class Car(models.Model):
    manufacturer = models.ForeignKey('production.Manufacturer')

This sort of reference can be useful
when resolving circular import
dependencies between two applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文