django: how to use many-to-many relationships in values()?
Posted
by
john
on Stack Overflow
See other posts from Stack Overflow
or by john
Published on 2010-12-31T18:42:33Z
Indexed on
2010/12/31
20:53 UTC
Read the original article
Hit count: 116
django
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.
© Stack Overflow or respective owner