【发布时间】:2021-07-18 19:12:27
【问题描述】:
我有两个模型,RetailLocation 和 Transaction,它们分别共享一对多的关系。我的目标是使用与该 RetailLocation 对应的最新交易的日期来注释 RetailLocation。
模型事务包括“r_loc_name”,它与 RetailLocation.name 完全相同。我认为这个字段是多余的,我希望有一个更好的解决方案,只使用 RetailLocation 中的 r_loc ForeignKey(和设置),但是我还没有弄清楚如何。
型号
class RetailLocation(models.Model):
name = models.CharField(max_length=200, null=True)
class Transaction(models.Model):
date = models.DateTimeField(null=True)
r_loc = models.ForeignKey(RetailLocation, null=True, on_delete=models.SET_NULL)
r_loc_name = models.CharField(max_length=200, null=True) # Likely redundant
尝试的代码
loc_latest = RetailLocation.objects.all().annotate(
latest_txn_time=Subquery(
Transaction.objects.filter(r_loc_name=OuterRef("name"))
.order_by("date")
.values("date")
.last()
)
)
另一个尝试
loc_latest = RetailLocation.objects.all().annotate(
latest_txn_time=Value(
Subquery(
RetailLocation.objects.get(name=OuterRef("name"))
.transaction_set.order_by("date")
.last()
.date
),
DateTimeField(),
)
)
我的目标是引用 RetailLocation.latest_txn_time,这将是引用该 RetailLocation 的最新交易的注释日期时间。
非常感谢
【问题讨论】:
标签: django join foreign-keys subquery one-to-many