On Wed, 2008-01-09 at 04:44 -0800, Mr T wrote: > I am a newbie at this Django development environment and my Python is > not as good as I would like it but I am still in the early stages of > learning, so any help is appreciated. > > I have stumbled over a block which I can't yet get over so if anyone > can help I thank you in advance. > > I need to display information from 2 models. I have written a query > for each but I don't know how I could combine them to display them as > one in my HTML pages. > Normally I would do a UNION of the 2 queries if it was just SQL but > Django does not allow me to do that so is there anyway I could do > something along those lines? > > I understand that table1.objects.all() does a SELECT * FROM table1, is > there a way to do something like SELECT table1.column1, table2.column2 > FROM table1, table2 WHERE table1.column1 = table2.column1.
It's very hard to answer this in generalities. If the two models are related in some way, there's normally a way to select all the modelB's that are related to modelA. If they aren't related, then you need to approach the problem from a different direction: you have two querysets (which is, admittedly, two database queries, but for two unrelated selects, the amount of work required is pretty much the same). Each queryset acts as an iterator, so if you want to pass the combined result to a template as single variable, you need to join the iterators together. One way to do that is with itertools.chain. Another way would be to pull all the results back into Python (list(queryset1) and list(queryset2)) and then join the two lists and sort them however you like. Perhaps if you can post a short example of some models that illustrate your problem, we can be a bit more concrete in reply. Regards, Malcolm -- No one is listening until you make a mistake. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

