Note that there are some explanatory texts on larger screens.

plurals
  1. POhelp using django's ORM for UNION query
    text
    copied!<p>i am making a history page for a website. The structure of my classes is something like this:</p> <p><img src="https://i.stack.imgur.com/gr6sV.png" alt="alt text"></p> <pre><code>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 </code></pre> <p>its quite simple... i have a <strong>Parent</strong> that has multiple <strong>Sons</strong> both can do <strong>actions</strong> on the website and they are all saved in the History table that has a reference to the Person that executed the action. The history table is something like:</p> <pre><code>date | action | person_id ---------------------------------------- 16-12-2010 | saved profile | 1 16-12-2010 | new child | 2 </code></pre> <p>for a Parent i need to display all his actions and the action of his sons Using sql it would be:</p> <pre><code>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 </code></pre> <p>but i have no idea how to do that using django's ORM. Myabe using two querys? a loop? Do you have any ideas? i'd really appreciate some help.. thanks in advance</p> <p>BTW: i'm using django 1.1</p> <p><strong>EDIT:</strong> i added the attributes in the classes. These are just examples, my tables have more attributes, but this is how django translate the relations into tables</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload