Django annotate Count does not work on Many to Many table -


i have hierarchy of django models (reduced essentials):

project/models.py

class project(models.model):      @property     def material(self):         return = m2m_assembly_components.objects.filter(assembly__room__project = self).aggregate(count=models.count('component'))   class room(models.model):      project = models.foreignkey("project", related_name="rooms")  class assembly(models.model):      room = models.foreignkey("room", related_name="assemblies")     components = models.manytomanyfield("material.component", through="m2m_assembly_components")  class m2m_assembly_components(models.model):      component = models.foreignkey("material.component")     assembly = models.foreignkey("assembly") 

material/models.py

class component(models.model):      name = models.charfield(max_length=200) 

i want list of component entries in m2m_assembly_components related project , number.

something "project 1 has 7 components of type , 3 components of type b"

to use material property in project class, list of m2m_assembly_components objects , count value of 1.

can give me hint i'm doing wrong!?

edit 07.042016 12:11

i got kind of working, not in nice way:

 @property  def material(self):      return = m2m_assembly_components.objects.filter(assembly__room__project = self).values('component__name','component__articlenumber').annotate(count=models.count('component')) 

unfortunately gives me dict instead of object , have define values need:

{'component__articlenumber': '021123', 'count': 17, 'component__name': 'abdeckrahmen, e2, anthrazit, 1-fach'} 


Comments