On 12/14/07, [EMAIL PROTECTED] < [EMAIL PROTECTED]> wrote: > > > Hello, > > I'm rather new to Django and I've got a problem with foreign keys and > relationships in the views.py. > > Suppose I would like to create a calendar application with multiple > users. There should be the possibility that multiple users are > participant of one appointment. > > That is the corresponding models.py: > > class Appointment(models.Model): > description = models.CharField(max_length=50) > start_timestamp = models.DateTimeField() > end_timestamp = models.DateTimeField() > > class AppointmentRole(models.Model): > description = models.CharField(max_length=20) > > class AppointmentParticipant(models.Model): > appointment = models.ForeignKey(Appointment) > participant_user = models.ForeignKey(User) > participant_role = models.ForeignKey(AppointmentRole) > > > My problem is how to retrieve all appointment details in a view for > the current user, suppose he has got the user_id = 1.
Given a User instance, say u1, and the models you have defined above, u1.appointmentparticipant_set gives you access to all the AppointmentParticipant model instances that refer to u1. See the docs here: http://www.djangoproject.com/documentation/db-api/#backward The raw SQL statement for that would be: > > SELECT a.*, b.* > FROM calendar_appointmentparticipant a > INNER JOIN calendar_appointment b on (a.appointment = b.id) > INNER JOIN calendar_appointmentrole c on (a.participant_role = c.id) > WHERE a.paticipant_user = 1; > > I tried select_related etc., but to be true I'm totally stuck. > > Perhaps someone can give me a hint how to write the view without using > raw SQL. > > Many thanks in advance. Unless I'm missing something, there's no need for raw SQL (I'm not sure what you tried select_related on?). The automatically generated reverse relationship should give you access to what you need. Karen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---