Re: Queryset of instances bound to particular ForeignKey

2007-08-03 Thread Benjamin Goldenberg

Hi Colin,
The problem is that there are multiple ForeignKeys in A bound to
instances of B, but I only want the instances of B bound to a specific
foreignkey. Does that make sense?

-Benjamin

On Aug 3, 12:17 am, Collin Grady <[EMAIL PROTECTED]> wrote:
> I feel silly for not trying this earlier, but it appears to work :)
>
> >>> B.objects.filter(a__isnull=False)
>
> [, ]
>
> Models used for this test:http://dpaste.com/15931/


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



Re: Queryset of instances bound to particular ForeignKey

2007-08-03 Thread James Bennett

On 8/3/07, Collin Grady <[EMAIL PROTECTED]> wrote:
> >>> B.objects.filter(a__isnull=False)
> [, ]

And as pointed out in our IRC discussion, that needs a distinct()
slapped on the end to weed out duplicate results ;)


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

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



Re: Queryset of instances bound to particular ForeignKey

2007-08-03 Thread Collin Grady

I feel silly for not trying this earlier, but it appears to work :)

>>> B.objects.filter(a__isnull=False)
[, ]

Models used for this test: http://dpaste.com/15931/


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



Re: Queryset of instances bound to particular ForeignKey

2007-08-03 Thread James Bennett

On 8/3/07, James Bennett <[EMAIL PROTECTED]> wrote:
> Foo.objects.extra(where=['id IN (SELECT %s FROM %s)' % Bar._meta.db_table])

Should have been:

> Foo.objects.extra(where=['id IN (SELECT foo_id FROM %s)' % 
> Bar._meta.db_table])

(sent before I realized I'd decided not to use quote_name() for the example)

-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

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



Re: Queryset of instances bound to particular ForeignKey

2007-08-03 Thread James Bennett

On 8/3/07, Collin Grady <[EMAIL PROTECTED]> wrote:
> He wants every B that has an A fkeyed to it.
>
> In other words, every instance of B where b.a_set.count() > 0

In which case what he wants is probably something like

ModelB.objects.filter(id__in=[o.id for o in ModelA.objects.all()])

Which is kind of scary in terms of what it'll do, since it has to
instantiate every single ModelA object and do an attribute lookup on
each. Using 'values()' instead of 'all()' and specifying just the
foreign key column is a bit better, but still instantiates one
dictionary and performs one key lookup in it for each ModelA object.

Probably the most efficient thing you'll get from using just the
Django ORM methods is something like the following. Assume the models
Foo and Bar as defined here:

class Foo(models.Model):
name = models.CharField(maxlength=250)

class Bar(models.Model):
name = models.CharField(maxlength=250)
foo = models.ForeignKey(Foo)

To look up all those Foo, and only those Foo, which are currently
being referenced by a Bar, do the following:

Foo.objects.extra(where=['id IN (SELECT %s FROM %s)' % Bar._meta.db_table])

This does the whole thing in one query and avoids instantiating any
intermediate objects along the way. Note that variations in DB
implementations of SQL may mean you need to use backend.quote_name()
in there.



-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

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



Re: Queryset of instances bound to particular ForeignKey

2007-08-02 Thread Collin Grady

Neither of those is correct.

He wants every B that has an A fkeyed to it.

In other words, every instance of B where b.a_set.count() > 0


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



Re: Queryset of instances bound to particular ForeignKey

2007-08-02 Thread [EMAIL PROTECTED]

In model B.. if you say
modela = models.ForeignKey(ModelA)

You could also say b.modela_set.all()

On Aug 2, 8:18 pm, Lucky B <[EMAIL PROTECTED]> wrote:
> If I gather correctly you want to see every ModelA that's bound to a
> particular ModelB b?
>
> ModelA.objects.filter(modelBs=b)
>
> That gives you what you want.
>
> On Aug 2, 7:20 pm, Benjamin Goldenberg <[EMAIL PROTECTED]> wrote:
>
> > Hi everyone,
> > I asked about this earlier today on IRC and no one knew of a way to do
> > it. Basically I have a model, let's call it ModelA with a ForeignKey
> > to an instance of ModelB. I would like QuerySet of all instances of
> > ModelB that are bound to that particular ForeignKey. Does anyone have
> > any idea of how to construct such a query?
>
> > Thanks,
> > Benjamin


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



Re: Queryset of instances bound to particular ForeignKey

2007-08-02 Thread Lucky B

If I gather correctly you want to see every ModelA that's bound to a
particular ModelB b?

ModelA.objects.filter(modelBs=b)

That gives you what you want.

On Aug 2, 7:20 pm, Benjamin Goldenberg <[EMAIL PROTECTED]> wrote:
> Hi everyone,
> I asked about this earlier today on IRC and no one knew of a way to do
> it. Basically I have a model, let's call it ModelA with a ForeignKey
> to an instance of ModelB. I would like QuerySet of all instances of
> ModelB that are bound to that particular ForeignKey. Does anyone have
> any idea of how to construct such a query?
>
> Thanks,
> Benjamin


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