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
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 thanksJoris{% 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...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
If this is not implemented as a OneToOneField, then do so. You can then
access the related object in the same way as the reverse of a ForeignKey
and use related_name to make this more intuitive.