简体   繁体   English

Django-如何在外键字段中使用过滤器?

[英]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). 其中LogEntry是我的模型,content_type是一个ForeignKey字段,该字段指向另一个具有字段ID和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'

Ahh, found it in another SO answer. 啊,在另一个SO答案中找到了它。 Weird it didn't seem to be documented. 很奇怪,似乎没有记录在案。 Or I just skimmed over it. 或者我只是略过而已。

Answer: 回答:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM