David542
David542

Reputation: 110382

How to get string on .values() to foreign key?

I would like to get all positions associated with a particular job post. JobPost is an FK to Position, and when I do normal .values(), I get the foreign key id rather than the position as a string. For example:

>>> JobPost.objects.filter(production=p).values('position')
[{'position': 4L}]

What I really need to get would be something like, in pseudocode:

>>> JobPost.objects.filter(production=p).values('position.position')

And the models:

class JobPost(models.Model):
    name = models.CharField(max_length=100)
    position = models.ForeignKey(Position)

class Position(models.Model):
    position = models.CharField(max_length=100)

How would I do this?

Upvotes: 0

Views: 390

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174662

JobPost.objects.filter(production=p).values('position__position')

See field lookups that span relationships.

Upvotes: 3

Related Questions