I got two models defined Owner and Dog
class Dog(models.Model):
name = models.CharField(max_length=255)
owner = models.ForeignKey(Owner)
adopted_date = models.DateTimeField()
class Owner(models.Model):
name = models.CharField(max_length=255)
I want to make a list with all the owners with the amount of dogs adopted by date.
Example: OwnerName Date Amount Richard 15/11/24 2 Jow 15/11/24 2
I am making this query
Dog.objects.values('owner', 'adopted_date').annotate(Count('owner')).order_by()
The problem here is that this query return owner id not the name.
{
'owner':1,
'adopted_date': 'date',
'owner_count': 2
}
I need to get on the owner the name of the owner, something like.
{
'owner':'Richard,
'adopted_date': 'date',
'owner_count': 2
}
Any help will be appreciated.
EDITION
This solution works, But I'm having a few doubts about it, making this query Dog.objects.values('owner__name', 'owner', 'adopted_date').annotate(Count('owner'))
We can get a good result, but I am worry about performance, this will generate a group by for every column inside values, but I don't need to group by owner_name because that column is not unique.
I am looking for something like
Dog.objects.values('owner', 'adopted_date').annotate(Count('owner'), Column('owner__name')).order_by()
I know that the Column aggregator function don't exist, but maybe is a way to do something like this.
Thanks in advance.