我正在为一个网站制作一个历史页面。我的类的结构是这样的:
class Person(models.Model):
name = models.CharField(max_length=100)
type = models.CharField(max_length=30)
class History(models.Model):
date = models.DateField(max_length=100)
action = models.CharField(max_length=250)
person = models.ForeignKey(Person)
class Parent(Person):
#some attributes that are not relevant
class Son(Person)
parent = models.ForeignKey(Parent)
#other attributes that are not relevant
很简单…我有一个
起源
有多重的
儿子
两者都可以
行动
在网站上,它们都保存在历史记录表中,该表引用了执行操作的人。历史记录表类似于:
date | action | person_id
----------------------------------------
16-12-2010 | saved profile | 1
16-12-2010 | new child | 2
作为一个家长,我需要展示他所有的行为和他儿子的行为。
使用SQL,它将是:
SELECT * FROM History where person_id=1
UNION
SELECT h.* FROM History h JOIN Son s ON s.person_ptr_id=h.person_id WHERE s.parent_id=1
但我不知道如何使用姜戈的ORM来实现这一点。Myabe使用两个查询?循环?
你有什么想法吗?我非常感谢你的帮助。提前谢谢
顺便说一句:我用的是姜戈1.1。
编辑:
我在类中添加了属性。这些只是示例,我的表有更多的属性,但Django就是这样将关系转换为表的。