Re: filtering the join table in a many-to-many relationship

2009-06-02 Thread Ben Adida

Sam,

Thanks for the rapid response. I think I see what you mean, but I'm
not sure it solves my problem.

Looking at the example you sent, here's how my problem would look in
that context:

- Friendship has an extra column called "degree_of_friendship", which
can be "meh", "good", or "great"
- I want to find all of Bob's "great" friends.

I can get Bob's great friendSHIPS by doing this:

Friendship.objects.filter(first = bob, degree_of_friendship = "great")

or I could get all of Bob's friends:

bob.friends.all()

but how do I get the Friend objects that are "great", but not others?

In other words, how do I impose a constraint on the Friendship join-
table when I do bob.friends?

-Ben

On Jun 1, 7:59 pm, Sam Chuparkoff  wrote:
> On Mon, 2009-06-01 at 16:01 -0700, Ben Adida wrote:
> > I have a ManyToMany self relationship with a custom join table (I
> > don't think the self-reference is important to the problem, but it
> > might be, so I'm mentioning it):
>
> > class Documents
> >   related_docs = models.ManyToManyField("self",
> > through='DocumentRels')
>
> > DocumentRels is a table that maps one document to another, with a
> > relationship type.
>
> > If I want all documents related to doc_0, I can just do:
>
> >   doc_0.related_docs
>
> > But what if I want all documents related to doc_0 where I specify an
> > extra constraint on the join table, e.g. a value for
> > DocumentRels.relationship? Is there a way to do that without custom
> > SQL?
>
> You will not need custom sql, because DocumentRels is going to be a
> Model. When you want to filter based on DocumentRels.relationship, you
> will either use:
>
>   DocumentRels.objects.filter()
>
> or go through the RelatedManagers that are added to your Document
> model for the ForeignKeys in DocumentRels. The reference is here:
>
> http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-o...
>
> For code that exactly fits your case, see PersonSelfRefM2M in:
>
> http://code.djangoproject.com/browser/django/trunk/tests/modeltests/m...
>
> sdc
--~--~-~--~~~---~--~~
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: filtering the join table in a many-to-many relationship

2009-06-01 Thread Sam Chuparkoff

On Mon, 2009-06-01 at 16:01 -0700, Ben Adida wrote:
> I have a ManyToMany self relationship with a custom join table (I
> don't think the self-reference is important to the problem, but it
> might be, so I'm mentioning it):
> 
> class Documents
>   related_docs = models.ManyToManyField("self",
> through='DocumentRels')
> 
> DocumentRels is a table that maps one document to another, with a
> relationship type.
> 
> If I want all documents related to doc_0, I can just do:
> 
>   doc_0.related_docs
> 
> But what if I want all documents related to doc_0 where I specify an
> extra constraint on the join table, e.g. a value for
> DocumentRels.relationship? Is there a way to do that without custom
> SQL?

You will not need custom sql, because DocumentRels is going to be a
Model. When you want to filter based on DocumentRels.relationship, you
will either use:

  DocumentRels.objects.filter()

or go through the RelatedManagers that are added to your Document
model for the ForeignKeys in DocumentRels. The reference is here:

http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

For code that exactly fits your case, see PersonSelfRefM2M in:

http://code.djangoproject.com/browser/django/trunk/tests/modeltests/m2m_through/models.py

sdc



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



filtering the join table in a many-to-many relationship

2009-06-01 Thread Ben Adida

Hi folks,

I've looked through the forum and can't find an answer to this one.

I have a ManyToMany self relationship with a custom join table (I
don't think the self-reference is important to the problem, but it
might be, so I'm mentioning it):

class Documents
  related_docs = models.ManyToManyField("self",
through='DocumentRels')

DocumentRels is a table that maps one document to another, with a
relationship type.

If I want all documents related to doc_0, I can just do:

  doc_0.related_docs

But what if I want all documents related to doc_0 where I specify an
extra constraint on the join table, e.g. a value for
DocumentRels.relationship? Is there a way to do that without custom
SQL?

Thanks!

-Ben

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---