Reputation: 73
I have 2 django models like this:
class UserProfile(models.Model):
user = models.OneToOneField(User)
organisation = models.CharField(max_length=200)
class Submission(models.Model):
user = models.ForeignKey(User)
date_submission = models.DateTimeField(db_index=True, default=datetime.now())
date_published = models.DateTimeField(db_index=True, null=True)
status = models.CharField(db_index=True, max_length=4000)
logfile = models.TextField(db_index=False)
where each 'Submission' object is owned by a normal django user, and each user has a UserProfile configured using the AUTH_PROFILE_MODULE in the normal way.
This works as you would expect, i can see the organisation field of the UserProfile object:
Submission.objects.all()[0].user.userprofile.organisation
When i want to serialize the Submissions list (for export to json) normally i use:
Submission.objects.all().values()
# or for more control of fields
Submission.objects.all().values('status', 'date_submission')
# to traverse a relationship.. get info from the User object
Submission.objects.all().values('user__username')
.. these work fine. BUT my problem is that i cannot:
Submission.objects.all().values('user__userprofile__organisation')
raise FieldError("Invalid field name: '%s'" % name)
django.core.exceptions.FieldError: Invalid field name: 'user__userprofile__organisation'
so it seems that the UserProfile is a 'special case'. This was discussed here: Django query : Call values() on user__userprofile
but the solution doesn't help me (i'm fairly sure..)
Is this a limitation of the values() method ? does anyone know a way to get the same output of values() but able to traverse the UserProfile model ?
thanks for any ideas
-i
Upvotes: 0
Views: 2809
Reputation: 73
Turns out upgrading to version 1.4 of Django solved the problem, the comment by Jingo suggests 1.3.1 would also be ok.
so now i can:
query_set = Submission.objects.filter()
query_set.values('user__userprofile__organisation')
[{'user__userprofile__organisation': u'organisation test 1'}]
cheers
-i
Upvotes: 2