Try building this query set in your view and use the .filter() method. For example,
relevant_data = ModelA.objects.filter(unique_field__in=ModelB.objects.all().values('unique_field')) https://docs.djangoproject.com/en/dev/ref/models/querysets/#in On Jul 30, 2012 7:27 AM, "Joris" <joris.bensc...@gmail.com> wrote: > Dear list, > > Apologies in advance if this is a FAQ. I did extensive searching through > this list and the django docs but have not been able to find a solution. > > I'm trying to display data from a postgres backend into a HTML table. Most > of this data comes from a single table. Some columns however come from a > highly complex raw SQL query that is very CPU-expensive to run. The two > datasets cannot be combined into a single model for performance reasons. > Both models do however have an identical unique field . > > As templates cannot do lookups into a dictionary or DB object, I now > resort to doing a nested 'for' loop, where the template cycles through the > whole dataset of model 1 for every record of model 2. Then if the unique > field obtained from model 2 is found in model 1, the other valeus from > model 1 are displayed. Below is the current code I use. > This sounds hopelessly inefficient. Would someone be able to point me to a > method to make this more logical? > > Many thanks > > Joris > > {% for GROUP in GROUPS %} > <tr> > <td>{{ GROUP.grnummer}}</td> > {% for FIN in FINISH %} > {% if FIN.grnummer = GROUP.grnummer > %}<td>{{FIN.synthesis_finish}}</td>{% endif %} > {% endfor %} > > </tr> > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/tqaZqXWURo0J. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.