Django - 如何使用带有外键字段的过滤器? [英] Django - How to use a filter with a foreign key field?

查看:322
本文介绍了Django - 如何使用带有外键字段的过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图这样做:

  LogEntry.objects.filter(content_type ='visitor')
\\ / pre>

其中LogEntry是我的模型,而content_type是一个ForeignKey字段,指向带有字段id的另一个表,而content_type(varchar) 。



如何按其他表格的值进行搜索?当我尝试运行以上它说:

  int()的无效字面值为10:'visitor'


解决方案

啊,在另一个SO答案中找到。很奇怪,它似乎没有记录。或者我只是撇开它。



答案:

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


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'

解决方案

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

这篇关于Django - 如何使用带有外键字段的过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆