On Mon, 2009-03-09 at 10:31:16 PM, Malcolm Tredinnick wrote:

>> Is it possible in a django view, to take objects from 2 different
>> models, which are related one to the other through a ForeignKey
>> (primary key of one model is foreign key in the other model), make
>> something similar to an SQL JOIN and have the collection of these
>> joined instances passed to the template through the render_to_response
>> shortcut? I need to do this specifically to show fields from both
>> models in the same template/view.
>
>This is exactly the situation covered by the tutorial ([1]), where the
>Choice model is relate to the Poll model by a ForeignKey. Displaying
>polls and related choices is covered in part 3, the models are set up in
>part 1.
>
>[1] http://docs.djangoproject.com/en/dev/intro/tutorial01/
>
>You didn't skip doing the tutorial did you?


Hi Malcolm, first of all thanks for your quick response

To be totally honest yes I have read everything but it was several months ago 
and now tried to move on from where I was.

However I coudln't help feeling a little embarrassed with your answer so I 
carefully went through the Poll/Choice example in the tutorial once more :p 
(just kidding)

Getting to the point I think I didn't describe the scenario quite well. Using 
the same Poll/Choice example, let's say first I have two models Poll and Choice 
(just as in the example).

Now let's say there's a third model called C, to which the Choice model relates 
through a ForeignKey as well (so, I modify the Choice model in the examlpe and 
make it have a foreign key to Poll as in the example, but now it also has a 
foreign key to model C, and we eliminate the "choice" field in the Choice 
model). In a more human way, let's say Choices are not just a varchar but can 
only be picked from a "list of possible choices" instead (model C). Model C 
happens to have a "description" field that will hold the meaning of each 
possible "choice" each instance of Choice might be referring to.

Now how can I make a join of C and Choice, so I can iterate through such a 
collection and show for each item in it say, fields "description" from model C 
as well as field "votes" from model Choice?

What I think I need to do in the view is, pass on to the template a collection 
of a new kind of object that will have, for each instance, fields "description" 
(from model C), "choice_id" and "votes" (from model Choice).

The question is: what's the best way to accomplish this? I think I'm not 
missing anything but in case I do, I apologize and would very much appreciate 
it if anyone helps me see the light :)

Thanks a lot
Regards
Mario


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

Reply via email to