Saturday, October 25, 2014

How to get related data on one to one field of a foreign key?

Hi,
I have a log table with a foreign key to the user table, and a userprofile table with a one to one field to the user table:

class Log(models.Model):
    user = models.ForeignKey(User)
    ...

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    picture = models.ImageField(upload_to='profile_images', blank=True)
    ... 

In my ListView I want to get the user profile for each log.  I know I can use select_related to get the related users of the log as follows:
queryset = Log.objects.select_related('user').order_by('created')

... but how do I get the related profiles and pass these on to the template, so I can for example display the picture for each log?

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5f2e11e0-bb82-4280-a63e-f90560ca37bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment