You are not logged in.

Change Language:

Freelancer? Consultant? Check out: WorkTrail - Time Tracking made easy
Board » Django » Templates » looping and related fields

I've been trying to figure this out for days and I'm not having any luck.

I have two models where one has a foreign key to the other.

My queryset look likes this:
hotfix_data = Hotfixes.objects.all().select_related('patch_id__name').filter(patch_id__name__isnull=False).order_by('-patch_id')[:30]

If I look at the sql this generates it looks correct and contains the foreign key lookup.

However I cannot figure out how to loop over hotfix_data in the template so that I can display the field names and values from this queryset.
I have been able to display the the fields and values from Hotfixes.objects.all but not for select_related('patch_id__name')
I know that related data is there but I do not know how to access it inside my loop

I've been searching formuns looking for an answer to this and so far have found nothing that works.

Please help

Please can you post your two models : Hotfixes and model of "patch_id" to understand better this problem?
class Patches(models.Model):
patch_id = models.IntegerField(primary_key=True)
name = models.CharField(unique=True, max_length=765, blank=True)

class Hotfixes(models.Model):
hotfix_id = models.IntegerField(primary_key=True)
patch = models.ForeignKey(Patches, null=True, blank=True)
review = models.IntegerField()

The issue is in the template if I use obj.values and iterate over it I cannot access the related 'name' field from Patches.
The only way I seem to be able to access the data is by calling it like so,

{% for o on obj %}
{{ o.patch_id.name }}

but then I would have to call every object by name in the template, which is really cumbersome and ugly
I think that the problem is in your query:
Hotfixes.objects.all().select_related('patch_id__name').filter(patch_id__name__isnull=False).order_by('-patch_id')[:30]
Select_related: "Returns a QuerySet that will automatically "follow" foreign-key relationships, selecting that additional related-object data when it executes its query"

In your code there are these lines:
{% for o on obj %}
{{ o.patch_id.name }}
So I think that you want only the list of Patches. In this way the solution is rewrite the queryset extraction:
obj = Patches.objects.filter(name__isnull=False).order_by('name')[:30]
I did not understand very well the problem ... but maybe you can use some of these ideas.
If you need more help sends more piece of codes.

INFO: For test the data try to put this on your template before {% for o on obj %} :
{{obj}}
{% for o on obj %} ....
If it shows something like this: [ ], the problem is in the query.
I think that the problem is in your query:
Hotfixes.objects.all().select_related('patch_id__name').filter(patch_id__name__isnull=False).order_by('-patch_id')[:30]
Select_related: "Returns a QuerySet that will automatically "follow" foreign-key relationships, selecting that additional related-object data when it executes its query"

In your code there are these lines:
{% for o on obj %}
{{ o.patch_id.name }}
So I think that you want only the list of Patches. In this way the solution is rewrite the queryset extraction:
obj = Patches.objects.filter(name__isnull=False).order_by('name')[:30]
I did not understand very well the problem ... but maybe you can use some of these ideas.
If you need more help sends more piece of codes.

INFO: For test the data try to put this on your template before {% for o on obj %} :
{{obj}}
{% for o on obj %} ....
If it shows something like this: [ ], the problem is in the query.
Actually what I want is 30 rows from the hofixes table along with the patch name from the patches table where hotfixes.patch = patches.patch_id. I've looked at the generated sql and the query is correct. The issues I'm running into is there seems to be no way to display the results of this query so that the related patch name is included.

I want to be able to use the data returned to populate a formset.

In this way the problem is in your HTML code I think:
You use this:
{% for o on obj %} 
  {{ o.patch_id.name }} 
{% endfor %}

but if "o" is an Hotfixes instance, from Hotfixes model I think that the fk to Patches is only "patch":
{% for o on obj %} 
  {{ o.patch.name }} 
{% endfor %}

Another think:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.select_related
Your django code for select related probably is not correct: from documentation yuo want to use this:
hotfix_data = Hotfixes.objects.all().select_related('patch__name').filter(patch__name__isnull=False).order_by('-patch_id')[:30]
and not this
hotfix_data = Hotfixes.objects.all().select_related('patch_id__name').filter(patch_id__name__isnull=False).order_by('-patch_id')[:30]





Powered by Sphene Community Tools