On 08/03/07, Alex Dong <[EMAIL PROTECTED]> wrote: > > Hi all, > > I have a data model like this: > * a `user` has multiple `questions` > * a `user` could give multiple `answer`s or `comment`s to any > `question`s. > So here, there are > * a one-to-many relationship from `user` to `question` > * a many-to-many relationship between `question` and `answer`, > `question` and `comment`. > Now I need to pull out a list of all the questions a given user has > either answered or commented on. I could do `user.question_set` or > `user.answer_set`. > > Now if I write a template like this: > ` {% for answer in user.answer_set %} > ... {{ answer.question. ... }} > {% endfor %} > ` > I could end up with showing the same questions many times. > > I know for sure that this task is not that difficult if I retreat to > using `JOIN` and use raw sql for my query. But I'm wondering does > Django offers a neat solution for this question without using raw > sql? > > Thanks, > Alex >
How about this: Question.objects.filter(answer__user_id=userID) - this should give you the question objects for which the user has given answers. Extend the filter with a Q clause to do the OR for comments. You may need to throw a .distinct() in there as well. I've got a similar thing that works, I'm just not sure about the exact syntax for your case. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---