I have two models, RetailLocation and Transaction, which share a one-to-many relationship respectively. I trying to annotate the total number of days (Count) a RetailLocation has any number of Transactions for. In doing so, I am filtering the Transaction.date field to just Date instead of Datetime, and trying to SELECT DISTINCT dates, but am running into an error "NotImplementedError: annotate() + distinct(fields) is not implemented."
Models
class RetailLocation(models.Model):
name = models.CharField(max_length=200, null=True)
class Transaction(models.Model):
date = models.DateTimeField(null=True)
r_loc = models.ForeignKey(RetailLocation, null=True, on_delete=models.SET_NULL)
Attempted Code
test = RetailLocation.objects.annotate(
days_operating=Subquery(
Transaction.objects.filter(r_loc=OuterRef("pk"))
.distinct('date__date')
.annotate(count=Count('pk'))
.values('count')
)
)
I tried to reference this solution in combination with an earlier post solved by Willem, but using distinct seems to cause the NotImplementedError referenced above. I believe there also might be a solution using Count( , distinct=True), but it wouldn't help unless I could distinct on date__date, as I am only trying to find days that any number of Transactions occurred on.
Thank you very much for your time.