stephan
stephan

Reputation: 2403

How to pass dictionary values from a django query, as a filter value in another query?

query 1:

nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code='user.zipcode').location).order_by('distance')[:9]

print nearestzips

>>>[<PostalCode=97202>, <PostalCode=97201>, <PostalCode=97215>, <PostalCode=97239>, <PostalCode=97214>]

query 2:

latest_entries = Entry.objects.filter(zipcode__in=nearestzips.values())

print latest_entries

>>>TypeError: Cannot use a multi-field GeoValuesQuerySet as a filter value.

I am trying to take 'nearestzips' which contains 5 postal codes, and query entries that match those postal codes, but I am getting the error above. any advice?

Upvotes: 0

Views: 1394

Answers (1)

Joseph
Joseph

Reputation: 13198

Rather than use .values(), use a .values_list() with whatever field you want to match on:

latest_entries = Entry.objects.filter(zipcode__in=nearestzips.values_list('myField', flat=True))

Upvotes: 2

Related Questions