Newbie Django queryset question

17 views
Skip to first unread message

Murilo Vicentini

unread,
Mar 15, 2012, 8:07:08 PM3/15/12
to django...@googlegroups.com
Hey guys, sorry if this is a newbie question, but I'm new at django and I'm having a real hard time trying to figure out how to solve this. So I have this model.
class Run(models.Model):
    speccpus = models.ForeignKey('Speccpu')
    hosts = models.ForeignKey('Host')

    Task = models.IntegerField()
    Path = models.CharField(max_length = 300)
    Date_Time = models.DateTimeField()

class Host(models.Model):
    Name = models.CharField(max_length = 100)
    ip_addr = models.IPAddressField()
    admin_ip_addr = models.IPAddressField()
    Location = models.CharField(max_length = 100)

class Speccpu(models.Model):
    Version = models.CharField(max_length = 50)
    Base_score = models.IntegerField()
    Peak_score = models.IntegerField()

So what i need to do is search a for an input in any of this fields, so they can return more than one row of a table, since i need to display on the screen a table containing the Run - Task, Path, Date_Time fields, also the Host Name field, and the Speccpu Base and peak score. I was thinking about doing something like an inner join of this three models, and then filtering. But i can't manage to do it. Does anyone have any ideia of how i can do it using the queryset API?

Shawn Milochik

unread,
Mar 15, 2012, 8:58:52 PM3/15/12
to django...@googlegroups.com
If you don't know which fields your users will be searching on in advance, you'll have to create Q objects and dynamically build your query in your view.

https://docs.djangoproject.com/en/1.3/topics/db/queries/#complex-lookups-with-q-objects


Murilo Vicentini

unread,
Mar 15, 2012, 11:04:15 PM3/15/12
to django...@googlegroups.com
But if I got it correctly I can use Q objects to make complex filters but they only apply to one model, what I need is a combination of the models. And since the search can return more than one (or even zero) objects I can't use get() to see the related objects.

Shawn Milochik

unread,
Mar 15, 2012, 11:09:44 PM3/15/12
to django...@googlegroups.com
You can certainly use Q objects to query across models, just as you can in a normal QuerySet, by using the double-underscore notation.

https://docs.djangoproject.com/en/1.3/topics/db/queries/#lookups-that-span-relationships


Murilo Vicentini

unread,
Mar 15, 2012, 11:22:24 PM3/15/12
to django...@googlegroups.com
Uhmmm, thank you a lot. That was what I was looking for! Sorry for wasting your time. One last question, does this double-underscore notation work at my html template? Because after filtering I will want to display the information of the different models at one row of my table.

Shawn Milochik

unread,
Mar 15, 2012, 11:32:27 PM3/15/12
to django...@googlegroups.com
On 03/15/2012 11:22 PM, Murilo Vicentini wrote:
Uhmmm, thank you a lot. That was what I was looking for! Sorry for wasting your time. One last question, does this double-underscore notation work at my html template? Because after filtering I will want to display the information of the different models at one row of my table.


Try it and find out. ^_^

Also, a word of caution for things like that -- they lead to amazingly slow pages. Make sure you read up on select_related before you go crazy referring to related items in your templates (or views, for that matter).

https://docs.djangoproject.com/en/1.3/ref/models/querysets/#select-related

I suggest ignoring the "depth" kwarg and using the method demonstrated with "room__building" as an example. But read the preceding text first to understand how it works in general.


Reply all
Reply to author
Forward
0 new messages