简体   繁体   中英

Django Rest framework serialization error

I am using Django 1.8.3 and DRF 2.4.6. My models.py is as follows:

class Prescription(models.Model):
    date_prescribed = models.DateTimeField()
    doctor = models.ForeignKey(Doctor)  
    pharmacy = models.ForeignKey(Pharmacy)

What I want in my views is a queryset which finds the count of prescriptions grouped by month for the last six months.My views.py is as follows:

class PrescripTrendListView(generics.ListAPIView):
    queryset = Prescription.objects.all()
    serializer_class = LineGraphSerializer

    def get_queryset(self):
        end_date = timezone.now()
        start_date = end_date - relativedelta(months=6)
        truncate_date = connection.ops.date_trunc_sql('month', 'date_prescribed')
        qs = super(PrescriptionTrendListView,self).get_queryset().extra(select={'month': truncate_date})
        return qs.filter(date_prescribed__range=(start_date, end_date)).annotate(pk_count=Count('pk')).order_by('month')


    def get(self, request, *args, **kwargs):
        graph_data = self.get_queryset().values('pk_count', 'month')
        serializer = self.get_serializer(data=graph_data, many=True)
        return Response(serializer.data)

My serializer.py is as follows:

class LineGraphSerializer(serializers.Serializer):
    pk_count = serializers.IntegerField(read_only=True)
    month = serializers.DateTimeField(read_only=True)

However I get an error stating that 'NoneType' object is not iterable .The full traceback is here . I am just completely clueless as to what is wrong..Please help

while seeing your code you are passing the different serializer_class in the view. adding the custome fields in the serializer.

serializers.py

class Prescriptionserializers(serializers.ModelSerializer):
    pk_count = serializers.IntegerField(read_only=True, required=False)
    month = serializers.DateTimeField(read_only=True, required=False)
    class Meta:
        model = Prescription
        fields = ('id', 'date_prescribed', 'doctor', 'pharmacy', 'pk_count', 'month')

views.py

class PrescripTrendListView(generics.ListAPIView):
    queryset = Prescription.objects.all()
    serializer_class = Prescriptionserializers

    # write the you reqirements here

I would add the filtering as a model manager, and then your serializer becomes a lot cleaner.

models.py:

class PrescriptionManager(models.Manager):
    def periodic_prescriptions(self):
        six_months = datetime.now() - timedelta(months=6)
        return super(PrescriptionManager, self).get_queryset().filter(
            timestamp__gte=six_months).order_by('-timestamp')


class Prescription(models.Model):
    date_prescribed = models.DateTimeField()
    doctor = models.ForeignKey(Doctor)  
    pharmacy = models.ForeignKey(Pharmacy)
    timestamp = models.DateTimeField(auto_now_add=True)

    objects = PrescriptionManager()

serializer:

class PrescripTrendListView(generics.ListAPIView):
    serializer_class = LineGraphSerializer

    def get_queryset(self):
        Prescription.objects.periodic_prescription()

I hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM