django: how to use many-to-many relationships in values()?
- by john
i need to group results by a field that requires a few joins from the original model:
// response_filter_args is created dynamically
responses = Response.objects.filter(**response_filter_args) \
.values('customer__tags__tag') \ # django doesn't like this
.annotate(average_score=Avg('rating__score'))
Response - customer - tags (many-to-many field pointing to Tag) - tag (the tag as a string)
Models are:
class Response(models.Model):
customer = models.ForeignKey(Customer)
...
class Customer(models.Model):
tags = models.ManyToManyField(Tag)
...
class Tag(models.Model):
tag = models.CharField(max_length=255)
...
i'm trying to calculate average ratings. to make it work i need to tell django to group by 'tag', but it refuses to. it gives an error:
Invalid field name: 'customer__tags__tag'
anyone know how i can get it to group by tag? i've tried all the combinations of underscores in customer_tags_tag that i can think of, but nothing works.