本文介绍了django外键包含查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下模型
class Command(models.Model):
server = models.ForeignKey(Server)
user_login = models.CharField(max_length=100)
user_run = models.CharField(max_length=100)
host = models.CharField(max_length=100)
ip = models.CharField(max_length=100)
session = models.CharField(max_length=100)
command = models.TextField()
ts = models.DateTimeField(auto_now_add=True)
version = models.CharField(max_length=100)
type = models.CharField(max_length=100)
我有以下搜索查询
cmds = Command.objects.filter(Q(user_login__contains=form.cleaned_data['loguser']),
Q(user_run__contains=form.cleaned_data['runuser']),
Q(host__contains=form.cleaned_data['loghost']),
Q(command__contains=form.cleaned_data['command']),
Q(server__contains=form.cleaned_data['host']),
Q(session__contains=form.cleaned_data['session'])) \
.order_by('-id')[:100]
我需要按以下字符串搜索服务器.host
I need to search by the following string for server.host
如果尝试添加以下内容,则会出现错误
If I try to add the following I get an error
Q(server__contains=form.cleaned_data['host']),
Exception Type: TypeError
Exception Value:
Related Field has invalid lookup: contains
Exception Location: /usr/lib/python2.5/site-packages/django/db/models/fields/related.py in get_db_prep_lookup, line 156
form.cleaned_data ['host']将包含主机名的文本字符串。
form.cleaned_data['host'] will contain a text string for a hostname.
推荐答案
server__searchfieldname__contains
您没有指定服务器表中的哪个字段应显示
you didn't specify what field in server table should look up.
这篇关于django外键包含查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!