Re: Joining foreign keys backwards?
On 3/18/06, Ned Batchelder <[EMAIL PROTECTED]> wrote: > > Or something like that, I'm backtranslating from my real app. Is there > a cleaner way? I think you can do this... Not very clean, but I just had lunch and I am feeling sleepy. poll_list = [] choice_list = choices.get_list(choice__exact = "overeasy", select_related = True) for choice in choice_list: poll_list.append(choice.get_poll()) The select_related = True argument will JOIN polls and choices avoiding a new database hit on every .get_poll() -- Julio Nobrega - http://www.inerciasensorial.com.br --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Joining foreign keys backwards?
Don, thank, but the relationship here is one-to-many. Are you suggesting artificially making it many-to-many to get the methods I need? --Ned. Don Arbow wrote: Check out the docs about many-to-many relationships. The link below talks exactly about what you are asking. http://www.djangoproject.com/documentation/db_api/#many-to-many- relations On Mar 18, 2006, at 8:05 AM, Ned Batchelder wrote: I want to select objects based on their relationships with related objects, but I don't seem to be able to do it, because I'm trying to use a foreign key in the "wrong" direction. For example, in the Polls and Choices model, I can do this (from the docs): choices.get_list(poll__slug__exact="eggs") This works because the Choice class has a poll ForeignKey. But I can't get a list of polls based on the characteristics of choices: polls.get_list(choice__choice__exact="overeasy") # Doesn't work. Am I wrong? Please tell me there is a way to accomplish what I want. This is all in .91. I can manage it with trapdoor params like where=: polls.get_list(where=["id in (SELECT poll_id FROM app_choices where choice='overeasy')"]) Or something like that, I'm backtranslating from my real app. Is there a cleaner way? Thanks. --Ned. -- Ned Batchelder, http://nedbatchelder.com . -- Ned Batchelder, http://nedbatchelder.com --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Joining foreign keys backwards?
Check out the docs about many-to-many relationships. The link below talks exactly about what you are asking. http://www.djangoproject.com/documentation/db_api/#many-to-many- relations On Mar 18, 2006, at 8:05 AM, Ned Batchelder wrote: > > I want to select objects based on their relationships with related > objects, but I don't seem to be able to do it, because I'm trying > to use > a foreign key in the "wrong" direction. For example, in the Polls and > Choices model, I can do this (from the docs): > >choices.get_list(poll__slug__exact="eggs") > > This works because the Choice class has a poll ForeignKey. But I > can't > get a list of polls based on the characteristics of choices: > >polls.get_list(choice__choice__exact="overeasy") # Doesn't work. > > Am I wrong? Please tell me there is a way to accomplish what I want. > This is all in .91. I can manage it with trapdoor params like where=: > >polls.get_list(where=["id in (SELECT poll_id FROM app_choices where > choice='overeasy')"]) > > Or something like that, I'm backtranslating from my real app. Is > there > a cleaner way? > > Thanks. > > --Ned. > > -- > Ned Batchelder, http://nedbatchelder.com > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Joining foreign keys backwards?
Ned Batchelder wrote: > polls.get_list(choice__choice__exact="overeasy") # Doesn't work. > >Am I wrong? Please tell me there is a way to accomplish what I want. >This is all in .91. > AFAIK this is one of the thing that magic-removal is solving. There you would have Poll.choice_set which is a query you want. In 0.91 if it only needed in a couple of models you can declare such functions manually: class Poll(meta.Model): ... def _module_get_for_choice(choice): polls.get_list(where=["id in (SELECT poll_id FROM app_choices where choice='%s')"%choice]) This _module_get_for_choice will magically become a method of module polls: polls.get_for_choice('overeasy') --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Joining foreign keys backwards?
I want to select objects based on their relationships with related objects, but I don't seem to be able to do it, because I'm trying to use a foreign key in the "wrong" direction. For example, in the Polls and Choices model, I can do this (from the docs): choices.get_list(poll__slug__exact="eggs") This works because the Choice class has a poll ForeignKey. But I can't get a list of polls based on the characteristics of choices: polls.get_list(choice__choice__exact="overeasy") # Doesn't work. Am I wrong? Please tell me there is a way to accomplish what I want. This is all in .91. I can manage it with trapdoor params like where=: polls.get_list(where=["id in (SELECT poll_id FROM app_choices where choice='overeasy')"]) Or something like that, I'm backtranslating from my real app. Is there a cleaner way? Thanks. --Ned. -- Ned Batchelder, http://nedbatchelder.com --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---