What I want to achieve:
I want a list of users with their respective missions, and filter on missions start date.
# Pseudo json
User 1
- mission 1
- mission 2
User 2
- mission 1
- mission 2
- mission 3
My data structure:
Models:
class Mission(models.Model):
start = models.DateTimeField()
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="missions")
Serializers:
# Mission
class MissionSerializer(serializers.ModelSerializer):
class Meta:
model = Mission
fields = (
'start',
'end',
)
# User
class UserSerializer(serializers.ModelSerializer):
missions = MissionSerializer(many=True)
class Meta:
model = MyUser
fields = (
'username',
'missions',
)
Viewsets:
# Filter
class UserFilter(django_filters.FilterSet):
class Meta:
model = MyUser
fields = {
'missions__start': ['gte','lt']
}
# Viewset
class UserViewset(viewsets.ModelViewSet):
filter_backends = (filters.OrderingFilter, filters.DjangoFilterBackend,)
filter_class = UserFilter
serializer_class = UserSerializer
@list_route(methods=['get'])
def listCalendar(self, request):
prefetched_missions = Prefetch('missions', queryset=Mission.objects.all())
objects_list = MyUser.objects.prefetch_related( prefetched_missions )
objects_list = self.filter_queryset(objects_list)
serializer = UserSerializer(objects_list, many=True)
return Response(serializer.data)
My Problem:
When calling this URL:
/api/users/listCalendar/?start__gte=2015-06-29&start__lt=2015-08-10
The filter is ignored and I can't figure out a way to make it work.
I have the intuition that the problem is with Mission.objects.all()
in the ViewSet that should probably be something like: Mission.objects.filter(*But what here?*)
Any help would be very much appreciated!
Edit 1:
There is some progress! But still not working... As you suggested Mark Galloway i tried calling the following url:
/api/users/listCalendar/?missions__start__gte=2015-06-29&missions__start__lt=2015-08-10
But this is the query that gets executed:
SELECT "app_myuser"."id", "app_myuser"."username"
FROM "app_myuser"
INNER JOIN "app_mission" ON ( "app_myuser"."id" = "app_mission"."user_id" )
INNER JOIN "app_mission" T4 ON ( "app_myuser"."id" = T4."user_id" )
WHERE ("app_mission"."start" >= '2015-07-06T00:00:00+00:00'::timestamptz
AND T4."start" < '2015-07-12T00:00:00+00:00'::timestamptz)
ORDER BY "app_myuser"."username" ASC;
As you can see, there are 2 INNER JOIN instead of 1. For some reasons it takes the 2 filtered fields as if they were in separate tables. The result is that my results are dupplicated.