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

[亡魂溺海] 提交于 2020-01-17 04:56:06

问题


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'

回答1:


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') 


来源:https://stackoverflow.com/questions/5517314/django-how-to-use-a-filter-with-a-foreign-key-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!