Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to filter queryset on calculated field provided by `extra` call in Django?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
dnedbaylo  
View profile  
 More options Nov 4, 4:03 pm
From: dnedbaylo <dmytro.nedba...@gmail.com>
Date: Wed, 4 Nov 2009 13:03:24 -0800 (PST)
Local: Wed, Nov 4 2009 4:03 pm
Subject: How to filter queryset on calculated field provided by `extra` call in Django?
Hi all,

Here is my django models:

class Author (models.Model):
    name = models.CharField(max_length=255)
    removed = models.BooleanField(default=False)

class Image (models.Model):
    author = models.ForeignKey(Author)
    name = models.CharField(max_length=255)
    height = models.PositiveIntegerField()
    width  = models.PositiveIntegerField()

Basically, i need to select each author who is not removed and who has
5 or less images with height equal to 100.

I use MySQL, and here is version info:

    mysql Ver 14.12 Distrib 5.0.67

Naturally, it would look like this:

Author.objects.filter(removed=False).extra(select={
    'imgcount': """SELECT COUNT(*)
                   FROM ormtest_image
                   WHERE height=100 AND
                         ormtest_image.author_id=ormtest_author.id"""

}).filter(imgcount__lte=5)

It does not work: "FieldError: Cannot resolve keyword 'imgcount' into
field. Choices are: id, image, name, removed"

OK, let's try where argument of extra method:

Author.objects.filter(removed=False).extra(select={
    'imgcount': """SELECT COUNT(*)
                   FROM ormtest_image
                   WHERE height=100 AND
                         ormtest_image.author_id=ormtest_author.id"""

}, where=['imgcount <= 5'])

It does not work as well: "OperationalError: (1054, "Unknown column
'imgcount' in 'where clause'")", since to filter data on calculated
field in MySQL you have to use HAVING clause.

Any ideas?

I tested this with Django 1.1 and latest version from trunk.

So far, i use this hack:

Author.objects.filter(removed=False).extra(select={
    'imgcount': """SELECT COUNT(*)
                   FROM ormtest_image
                   WHERE height=100 AND
                         ormtest_image.author_id=ormtest_author.id"""


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google