How to write a 'from clause' in django?
Suppose there are two models m1 and m2, and now we need to ru开发者_开发知识库n a query equal to "SELECT * FROM m1, m2" in django. How to do it? Thank you.
I suppose you realise that the SQL you've posted produces a cartesian join between the tables m1 and m2? That is, your result set includes all possible rows from m1 joined with all possible rows from m2, so if you have 5 rows in one table and 10 in another, you'll get 50 results.
If that's really what you want, and I can't think why you would, there's no easy way to do it in the Django ORM.
However, if what you actually meant was a simple JOIN:
SELECT * from m1, m2 WHERE m1.m2_id = m2.id;
then you can achieve something similar with the select_related()
method.
EDIT:
m1.objects.all() + m2.objects.all()
list(m1.objects.all()) + list(m2.objects.all())
But.. Daniel Roseman has a very good point about Cartesian product - if that's what you meant.
精彩评论