many to many - Django: Reverse M2M query with a "through" model -


i have following setup:

class ride(models.model):    ...    riders = models.manytomanyfield(user, through='rideriders', null=true, blank=true)     class meta:         db_table    = 'rides'         app_label   = 'rides'   class rideriders(models.model):    user = models.foreignkey(user)    ride = models.foreignkey(ride)     # other information    ...     class meta:        db_table    = 'rides_riders'        app_label   = 'rides' 

so, example, can rides user using following (is best way?):

ride.objects.filter(riders__id=user_id) 

i want able users particular ride can't seem figure out how. rideriders model seems sticking point can't seem pass beyond user. example:

ride.objects.get(id=ride_id).riders.all() 

...gives list of riderider objects (i think).

any suggestions?

edit: add more context, i'm attempting fetch riders particular ride api call using django rest framework. how i'm using it:

class rideriders(generics.listapiview):    model = user    serializer_class = riderserializer     def get_queryset(self):        return ride.objects.get(id=self.kwargs.get('id')).riders.all() 

but giving me error why feel it's returning riderider objects rather rider objects.

fielderror: cannot resolve keyword u'ride' field. choices are: date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, user_permissions, username 

edit 2: ride.objects.get(id=self.kwargs.get('id')).riders.all() throwing above error (nothing serializers thought). if run ride.objects.get(id=self.kwargs.get('id')).rideriders_set.all() returns queryset (and testing gives expected number of records particular ride). so, leaves original question of how return list of users (rather riderider objects)?

the culprit turned out app_label = 'rides' in model classes incorrect after refactoring of overall application. stopping django making proper lookups across models weren't being registered properly.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -