Such a filter will actually select every instance of ModelA with *at least* one related instance of ModelB which matches your filter. The filter will not exclude, in the resulting queryset, the related instances of ModelB that do not match the filter.
Em qui, 11 de jul de 2019 às 06:41, Василий Русин <[email protected]> escreveu: > *models.py* > > > > > > > > *class ModelA(models.Model): views = > models.PositiveBigIntegerField()class ModelB(models.Model): parent = > models.ForeignKey(ModelA, on_delete=models.CASCADE, related_name='modelB', > blank=True, null=True) string = models.CharField()* > > > *views.py* > > > > > > > > > > > > > > > > *class ModelAListView(generics.ListAPIView): serializer_class = > ModelASerialezer queryset = > ModelA.objects.all().prefetch_related('modelb') def list(self, request, > *args, **kwargs): queryset = > self.filter_queryset(self.get_queryset()) page = > self.paginate_queryset(queryset) if page is not None: > serializer = self.get_serializer(page) return > self.get_paginated_response(serializer.data) serializer = > self.get_serializer(queryset.filter(modelb__string__icontains=request.GET['string']), > many=True) return Response(serializer.data)* > > *serializers.py* > > > > > > > > > > > > > > > > > *class ModelASerializer(serializers.ModelSerializer): id = > serializers.ReadOnlyField() modelB = ModelBSerializer(source='modelB', > many=True, read_only=False) class Meta: model = ModelA > exclude = ('views',)class ModelBSerializer(serializers.ModelSerializer): > id = serializers.IntegerField(required=False) class Meta: model = > ModelB fields = '__all__'* > If I need to search by "string" field I can write: > > modelA.objects.filter(modelB__string__icontains=request.GET['string']).values('modelB__string') > > > > Which return ModelB instances *only with necessary **string **values*: > > <QuerySet [{'modelB__string': 'Test1'}]> > > > > When I filter by modelb_string I expect to get only filtered FK value: > > { > "id": 1, > "views": 0, > "modelb": [ > { > "id": 46, > "string": "Test1", > "item": 1 > } > ] > } > > > > *but *I get all FK values: > > { > "id": 1, > "views": 0, > "modelb": [ > { > "id": 46, > "string": "Test1", > "item": 1 > }, > { > "id": 47, > "string": "Test85", > "item": 1 > }, > { > "id": 48, > "string": "Test64", > "item": 1 > } > ] > } > > > > -- > You received this message because you are subscribed to the Google Groups > "Django REST framework" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-rest-framework/e18c0ced-b487-4c03-b832-570f68b98d8e%40googlegroups.com > <https://groups.google.com/d/msgid/django-rest-framework/e18c0ced-b487-4c03-b832-570f68b98d8e%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CAMpbhxwbru1r1YcoOfQDyqLE%2BqmiHf-MQWyKD%2BAuvif737t67Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
