I am trying to count daily records for some model, but I would like the count was made only for records with some fk field = xy so I get list with days where there was a new record created but some may return 0.
class SomeModel(models.Model):
place = models.ForeignKey(Place)
note = models.TextField()
time_added = models.DateTimeField()
Say There's a Place with name="NewYork"
data = SomeModel.objects.extra({'created': "date(time_added)"}).values('created').annotate(placed_in_ny_count=Count('id'))
This works, but shows all records.. all places.
Tried with filtering, but it does not return days, where there was no record with place.name="NewYork". That's not what I need.
data = SomeModel.objects.extra({'created': "date(time_added)"}).values('created').annotate(placed_in_ny_count=Count('id'))len(data)gives 153. Adding.filter(place=Place.objects.get(name='NewYork'))returns less as it only returns days when SomeModel record with the 'place' was created. I want days without such records returned too. I would like to do something likeny_counts=Count('id' where place=Place.objects.get(name='NewYork'))and get[{'placed_in_ny_count': 23, 'created': datetime.date(2012,1,12)}, {'placed_in_ny_count': 0, 'created': datetimedate(2012,1,13)}...]