This seems so blatant that it couldn't really be a bug without someone
noticing. Django 1.2

        qs = Apt.objects.filter(list_on_web=True,is_available=True)

         # FastAdderStatus has Apt and Service as fk
        qs = qs.filter(fastadderstatus__service=self.service)

        # later on here's another filter on the same table
        qs = qs.filter(fastadderstatus__running_status__in= (2, 3, 4))

produces this:

  SELECT U0."id" FROM "nsproperties_apt" U0 INNER JOIN
"fastadder_fastadderstatus" U1 ON (U0."id" = U1."apt_id") INNER JOIN
"fastadder_fastadderstatus" U3 ON (U0."id" = U3."apt_id") WHERE
(U0."list_on_web" = True  AND U0."is_available" = True  AND
U1."service_id" = 4  AND U3."running_status" IN (2, 3, 4))

Note that it joins the same table twice, and the Apt is not returning
distinct.  Postgres is not liking the query.

count: 2837

[<Apt: Property #12297>, <Apt: Property #12297>, <Apt: Property
#12297>, <Apt: Property #12304>, <Apt: Property #12304>, <Apt:
Property #12304>, <Apt: Property #12305>, <Apt: Property #12305>,
<Apt: Property #12305>, <Apt: Property #12308>, <Apt: Property
#12308>, <Apt: Property #12308>, <Apt: Property #12309>, <Apt:
Property #12309>, <Apt: Property #12309>, <Apt: Property #12311>,
<Apt: Property #12311>, <Apt: Property #12311>, <Apt: Property
#12314>, <Apt: Property #12314>, '...(remaining elements
truncated)...']

what it should say (manually fixed) :

  SELECT U0."id" FROM "nsproperties_apt" U0 INNER JOIN
"fastadder_fastadderstatus" U1 ON (U0."id" = U1."apt_id") WHERE
(U0."list_on_web" = True  AND U0."is_available" = True  AND
U1."service_id" = 4  AND U1."running_status" IN (2, 3, 4))

count: 611, no dups


My solution:

        params = {}
        if self.service:
            params['fastadderstatus__service'] = self.service
        if self.agent:
            params['agents'] = self.agent

        if self.status:
 
params.update( 
FastAdderStatus.objects.filter_params_for_status(self.status,'fastadderstatus') 
)

        # a single call to filter
        qs = qs.filter(**params)

which avoids the (?) bug and also is vastly more efficient.

Having just stepped through with the debugger and seen how much work
is involved with .filter I am in future going to avoid any multiple
calls to filter. Its better to pass around dicts.


SELECT U0."id" FROM "nsproperties_apt" U0 INNER JOIN
"fastadder_fastadderstatus" U1 ON (U0."id" = U1."apt_id") INNER JOIN
"nsproperties_apt_agents" U2 ON (U0."id" = U2."apt_id") WHERE
(U0."is_available" = True  AND U0."list_on_web" = True  AND
U1."running_status" IN (2, 3, 4) AND U2."agent_id" = 175  AND
U1."service_id" = 4 )

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to