2009-04-21 58 views
9

我有以下型號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] 

我需要如果我對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']將包含主機名的文本字符串。

回答

16
server__searchfieldname__contains 

您沒有指定服務器表中的哪個字段應查找。

+0

嘿你能提供一些文件鏈接嗎? – 2017-04-04 15:48:42