【问题标题】:django foreignkey contains querydjango外键包含查询
【发布时间】:2010-10-20 20:54:04
【问题描述】:

我有以下型号

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]

我需要通过以下字符串搜索 server.host

如果我尝试添加以下内容,则会出现错误

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'] 将包含主机名的文本字符串。

【问题讨论】:

    标签: django contains


    【解决方案1】:
    server__searchfieldname__contains
    

    您没有指定应该查找服务器表中的哪个字段。

    【讨论】:

    • 嘿,你能提供一些文档链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2018-09-01
    • 2014-07-10
    • 2014-01-21
    • 2020-05-09
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多