Home > Software engineering >  Django DRF get_queryset, how to join two models?
Django DRF get_queryset, how to join two models?

Time:08-19

I have two models, I have to make an endpoint where the results of two tables should appear in the json, which have a fongeringkey that joins them.

My code is the following: models.py

class Property(models.Model):
    address = models.CharField(max_length=120)
    city = models.CharField(max_length=32)
    price = models.BigIntegerField()
    description = models.TextField(blank=True, null=True)
    year = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'property'

class StatusHistory(models.Model):
    property = models.ForeignKey(Property, on_delete=models.CASCADE)
    status = models.ForeignKey(Status, on_delete=models.CASCADE)
    update_date = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'status_history'

views.py

class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = "page_size"
    max_page_size = 1000

class PropertyListView(viewsets.ModelViewSet):
    http_method_names = ['get', 'head']
    serializer_class = PropertyListSerializer
    queryset = Property.objects.all()
    pagination_class = StandardResultsSetPagination

    def get_serializer_class(self):
        if self.action == 'list':
            return PropertyListSerializer
        return PropertyListSerializer

    def get_queryset(self):
        queryset = Property.objects.all()
        if self.request.GET.get('year'):
            queryset = queryset.filter(year=self.request.GET.get('year'))
        if self.request.GET.get('city'):
            queryset = queryset.filter(city=self.request.GET.get('city'))
        if self.request.GET.get('status'):
            #what would I do here?
            pass
        
        else:
            queryset = queryset.order_by('-year')
        return queryset

    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
            
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

serializers.py

class PropertyListSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Property
        fields = ('id', 'address', 'city', 'price', 'description', 'year')

class StatusHistoryListSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = StatusHistory
        fields = ('property', 'status', 'update_date')

I don't know how to filter by get with the parameters, city, year(model Property) and status(model StatusHistory).

CodePudding user response:

You only need to filter based in the status of the history you can do the following:

if self.request.GET.get('status'):
    queryset = queryset.filter(statushistory__status=self.request.GET.get('status'))
  • Related