Saturday, May 31, 2014

Django Wrong OUTER JOIN

Hi everyone,

I'm just starting with Django and I created the following queryset but I can't seem to make the JOIN connect to the one I've defined in my queryset values(..., 'user__uservote__event', ...)

My model declaration:


class UserVote(TimeStampedModel):
    event = models.ForeignKey('events.Event')
    fightchoice = models.ForeignKey('fights.FightChoice')
    user = models.ForeignKey(User)
 
class UserEvent(TimeStampedModel):
    event = models.ForeignKey('events.Event')
    user = models.ForeignKey('users.User')

My queryset:

user_season_events = UserEvent.objects\
    .filter(event__season=season_id, user=request.user.id, user__uservote__user=request.user.id)\
    .order_by('-event__event_on') \
    .select_related('event')\
    .values('event', 'user__uservote__fightchoice__points', 'user__uservote__event',
            'user__uservote__fightchoice__howitended', 'user__uservote__fightchoice__fighter') 

Return the following SQL: 

SELECT "users_userevent"."event_id", "fights_fightchoice"."points", "users_uservote"."event_id", "fights_fightchoice"."howitended_id", "fights_fightchoice"."fighter_id" 
FROM "users_userevent" 
INNER JOIN "events_event" ON ( "users_userevent"."event_id" = "events_event"."id" ) 
INNER JOIN "users_user" ON ( "users_userevent"."user_id" = "users_user"."id" ) 
LEFT OUTER JOIN "users_uservote" ON ( "users_user"."id" = "users_uservote"."user_id" ) 
LEFT OUTER JOIN "fights_fightchoice" ON ( "users_uservote"."fightchoice_id" = "fights_fightchoice"."id" ) 
WHERE ("events_event"."season_id" = 1 AND "users_userevent"."user_id" = 199 AND "users_uservote"."user_id" = 199 ) ORDER BY "events_event"."event_on" DESC

My issue is: 

I want:

LEFT OUTER JOIN "users_uservote" ON "users_user"."id" = "users_uservote"."user_id" )

to be (on events_event not users_user) like this:

LEFT OUTER JOIN users_uservote ON events_event.id = users_uservote.event_id

When I added 'user__uservote__event' to the values in my queryset should it not create it?

Thank you for you help.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9c9ae9bc-b92a-4fd6-8f1f-15a03eb29469%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment