Re: Obtaining objects from a many-to-many relationship

2012-06-02 Thread Thomas Lockhart

On 12-06-02 1:15 AM, Kurtis Mullins wrote:

Have you tried b.haveone.all()?

Yes. Simplemindedly I wanted to try

B.haveone.all()

If I understand your suggestion, it would require iterating on instances 
of B to find all instances of A mentioned in B.


that would be something like

for b in B.objects.all():
  a = b.haveone.all()
  ...

which of course does work fine but may require visiting an instance of A 
more than once.


What I'm looking for is to select all unique instances of A which appear 
in B without iterating on B. Not all instances of A appearing in b (one 
specific instance of B).


tia

 - Tom



On Sat, Jun 2, 2012 at 12:36 AM, Thomas Lockhart 
> wrote:


I've got two models with one having a many-to-many relationship
with the other:

class A(models.Model):
 name = models.CharField("name")

class B(models.Model):
 haveone = models.ManyToManyField(A)

What is the idiom for getting all instances of A which are
referenced in B? I found this works:

A.objects.annotate(n=models.Count('b')).filter(n__gt=0)

But istm that there is probably a more concise and straightforward
way to accomplish this (and the solution is probably obvious but
it is escaping me). tia

 - Tom

-- 
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
django-users+unsubscr...@googlegroups.com
.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.


--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Obtaining objects from a many-to-many relationship

2012-06-02 Thread Kurtis Mullins
https://docs.djangoproject.com/en/dev/topics/db/queries/#many-to-many-relationshipsfor
more information. (Unless I read your question wrong :))

On Sat, Jun 2, 2012 at 4:15 AM, Kurtis Mullins wrote:

> Have you tried b.haveone.all()?
>
>
> On Sat, Jun 2, 2012 at 12:36 AM, Thomas Lockhart 
> wrote:
>
>> I've got two models with one having a many-to-many relationship with the
>> other:
>>
>> class A(models.Model):
>>  name = models.CharField("name")
>>
>> class B(models.Model):
>>  haveone = models.ManyToManyField(A)
>>
>> What is the idiom for getting all instances of A which are referenced in
>> B? I found this works:
>>
>> A.objects.annotate(n=models.**Count('b')).filter(n__gt=0)
>>
>> But istm that there is probably a more concise and straightforward way to
>> accomplish this (and the solution is probably obvious but it is escaping
>> me). tia
>>
>>  - Tom
>>
>> --
>> 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 django-users+unsubscribe@**
>> googlegroups.com .
>> For more options, visit this group at http://groups.google.com/**
>> group/django-users?hl=en
>> .
>>
>>
>

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Obtaining objects from a many-to-many relationship

2012-06-02 Thread Kurtis Mullins
Have you tried b.haveone.all()?

On Sat, Jun 2, 2012 at 12:36 AM, Thomas Lockhart wrote:

> I've got two models with one having a many-to-many relationship with the
> other:
>
> class A(models.Model):
>  name = models.CharField("name")
>
> class B(models.Model):
>  haveone = models.ManyToManyField(A)
>
> What is the idiom for getting all instances of A which are referenced in
> B? I found this works:
>
> A.objects.annotate(n=models.**Count('b')).filter(n__gt=0)
>
> But istm that there is probably a more concise and straightforward way to
> accomplish this (and the solution is probably obvious but it is escaping
> me). tia
>
>  - Tom
>
> --
> 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 django-users+unsubscribe@**
> googlegroups.com .
> For more options, visit this group at http://groups.google.com/**
> group/django-users?hl=en
> .
>
>

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Obtaining objects from a many-to-many relationship

2012-06-01 Thread Thomas Lockhart
I've got two models with one having a many-to-many relationship with the 
other:


class A(models.Model):
  name = models.CharField("name")

class B(models.Model):
  haveone = models.ManyToManyField(A)

What is the idiom for getting all instances of A which are referenced in 
B? I found this works:


A.objects.annotate(n=models.Count('b')).filter(n__gt=0)

But istm that there is probably a more concise and straightforward way 
to accomplish this (and the solution is probably obvious but it is 
escaping me). tia


  - Tom

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.