Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django foreignkey contains query

I have the following model

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)

I have the following search query

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]

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'] will contain a text string for a hostname.

like image 401
Mike Avatar asked Apr 21 '09 15:04

Mike


1 Answers

server__searchfieldname__contains

you didn't specify what field in server table should look up.

like image 151
zalew Avatar answered Oct 17 '22 08:10

zalew