【问题标题】:Merging a field in one model to another with ForeignKey, Django Subquery and OuterRef使用 ForeignKey、Django 子查询和 OuterRef 将一个模型中的字段合并到另一个模型中
【发布时间】: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


    【解决方案1】:

    您可以在主键上使用OuterRef,并使用切片 ([:1]) 来获取最后一项,所以:

    loc_latest = RetailLocation.objects.annotate(
        latest_txn_time=Subquery(
            Transaction.objects.filter(
                r_loc=OuterRef('pk')
            ).order_by('-date').values('date')[:1]
        )
    )

    r_loc_name 确实是多余的。对于Transaction 对象,您可以使用my_transaction.r_loc.name,您可以使用Transaction.objects.filter(r_loc__name='some-name') 进行过滤等。

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 2021-03-26
      • 2020-12-14
      • 1970-01-01
      • 2017-12-11
      • 2017-04-17
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多