My models.py is as follows:
class Prescription(models.Model):
pr_id = models.CharField()
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey('Doctor')
pharmacy = models.ForeignKey('Pharmacy')
class Doctor(models.Model):
name = models.CharField()
age = models.IntegerField()
class Pharmacy(models.Model):
name = models.CharField()
status = models.CharField()Now i need to get the count of all pr_id grouped by month starting from this month and going back 6 months.This data needs to be in json as it needs to be sent to Angular to render a line chart. My views.py is as follows:
class PrescriptionTrendListView(generics.ListAPIView):
end_date = datetime.utcnow().date()
start_date = end_date + relativedelta(months=-6)
queryset = (Prescription.objects.extra(select={'month': connection.ops.date_trunc_sql('month', 'date_prescribed')})
.filter(date_prescribed__range=(start_date,end_date))
.values('month')
.annotate(Count('pr_id'))
.order_by('month'))
serializer_class = LineGraphSerializerThe queryset works because I tried it in the shell and it gives me the correct data as follows:
>>> queryset
[{'pr_id__count': 16, 'month': datetime.datetime(2015, 2, 1, 0, 0, tzinfo=<UTC>)},
{'pr_id__count': 71, 'month': datetime.datetime(2015, 3, 1, 0, 0, tzinfo=<UTC>)},
{'pr_id__count': 75, 'month': datetime.datetime(2015, 4, 1, 0, 0, tzinfo=<UTC>)},
{'pr_id__count': 96, 'month': datetime.datetime(2015, 5, 1, 0, 0, tzinfo=<UTC>)},
{'pr_id__count': 99, 'month': datetime.datetime(2015, 6, 1, 0, 0, tzinfo=<UTC>)},
{'pr_id__count': 93, 'month': datetime.datetime(2015, 7, 1, 0, 0, tzinfo=<UTC>)},
{'pr_id__count': 39, 'month': datetime.datetime(2015, 8, 1, 0, 0, tzinfo=<UTC>)}]However,I am not sure how to create the LineGraphSerializer. The two fields that the queryset gives are the count of the Ids and the months which are not in the initial model.Would you have any idea as to how would I go about creating this file.Any help will be greatly appreciated.
Regards,
Arjun
class LineGraphSerializer(serializers.Serializer):
count = serializers.IntegerField(source='pr_id__count')
month = serializers.SerializerMethodField()
def get_month(self, obj):
return '{0}-{1}'.format(obj.month.year, obj.month.month)--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/wkge9i0PKNI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class Prescription(models.Model):
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
class Doctor(models.Model):
name = models.CharField()
age = models.IntegerField()
class Pharmacy(models.Model):
name = models.CharField()
status = models.CharField()class PrescriptionTrendListView(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'from rest_framework import serializers
from .models.Prescription import Prescription
class LineGraphSerializer(serializers.ModelSerializer):
pk_count = serializers.IntegerField(read_only=True)
month = serializers.DateTimeField(read_only=True)
class Meta:
model = Prescription
fields = ['pk_count', 'month']
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.
--Regards,Arjun
class LineGraphSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return {
'pk_count': obj['pk_count'],
'month': obj['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)...
--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/wkge9i0PKNI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/wkge9i0PKNI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.
...--Regards,Arjun--