Greg
Greg

Reputation: 47094

Django - How to use a filter with a foreign key field?

I'm trying to do this:

LogEntry.objects.filter(content_type='visitor')

Where LogEntry is my model and content_type is a ForeignKey field pointing to another table with field id, and content_type (varchar).

How can I search by the value of the other table? When I try to run the above it says:

invalid literal for int() with base 10: 'visitor'

Upvotes: 0

Views: 1522

Answers (1)

Greg
Greg

Reputation: 47094

Ahh, found it in another SO answer. Weird it didn't seem to be documented. Or I just skimmed over it.

Answer:

LogEntry.objects.filter(content_type__name='visitor') 

Upvotes: 3

Related Questions