Django:按ID [多对一关系]获取最后一条记录 [英] Django: Get last record by ID [many-to-one relationship]
本文介绍了Django:按ID [多对一关系]获取最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Django模型中的多对一关系将一个表中的最后一条记录连接到另一个表.这是我的Django模型:
I'm trying to get the last record in one table connected to another one using many-to-one relationship in django models. Here's my django models:
class DataCollecttion(models.Model):
default_name = models.CharField(max_length=100)
class NameHistory(models.Model):
old_name = models.CharField(max_length=100)
collection_data = models.ForeignKey(DataCollection, on_delete=models.CASCADE, null=True)
在这里,我为 DataCollection
表创建了一个示例数据:
Here I created a sample data for DataCollection
table:
这是 NameHistory
表的示例数据:
我在这里想要过滤或获取每个 collection_data_id
中的 NameHistory
中的最后一条记录(红色矩形内的记录),并将其显示在我的视图中.简而言之,我想获得这些行以及如何在ORM查询中做到这一点:
What I want here is to filter or get the last record in NameHistory
in each collection_data_id
(the records inside the red rectangle) and display it in my views.
So in short I want to get these lines and how can I do it in ORM Query:
sample3
test2
data1
推荐答案
查看全文