Reputation: 7410
My models.py looks like this :
class Prescription(models.Model):
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
class Doctor(models.Model):
name = models.CharField(max_length=150)
age = models.PositiveSmallIntegerField()
class Pharmacy(models.Model):
name = models.CharField(max_length=150)
status = models.CharField()
In my views,I want the count of prescriptions grouped by month.I am using Django rest framework.My views.py is as follows:
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().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)
However,the error I get when I run this is :
I am assuming this has something to do with new style classes in Python 3.0.But that is all i know.Any help on how to fix this on 2.x ?
Upvotes: 0
Views: 79
Reputation: 6893
Your super()
call in get_queryset
is missing the type
and self
arguments.
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')
# Bug was here
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)
https://docs.python.org/2/library/functions.html#super
Upvotes: 1
Reputation: 1963
This is really stupid but I suspect it's because you're referencing Doctor and Pharmacy before you declare them. Try this instead:
class Doctor(models.Model):
name = models.CharField(max_length=150)
age = models.PositiveSmallIntegerField()
class Pharmacy(models.Model):
name = models.CharField(max_length=150)
status = models.CharField()
class Prescription(models.Model):
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
I looked at some of my old Django on Python 2.x and your models look fine, just that seems to be an issue maybe(?)
Upvotes: 0
Reputation: 21854
In Python 2, syntax for super
is this:
class MyClass(Base):
def func(self, *args, **kwargs):
super(MyClass, self).func(*args, **kwargs)
Upvotes: 1