I have a hierarchy of Django models (reduced to the 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 to get a list of all component entries in m2m_Assembly_Components which are related to a certain Project and their number.
Something like "Project 1 has 7 Components of type A and 3 Components of type B"
To do so I use the material property in the Project class, but I just get a list of m2m_Assembly_Components objects and a count value of 1.
Can anybody give me a hint what I'm doing wrong!?
Edit 07.042016 12:11
I got it kind of working, but not in a 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 this gives me a dict instead of an object and i have to define all the values i need:
{'component__articlenumber': '021123', 'count': 17, 'component__name': 'Abdeckrahmen, E2, anthrazit, 1-fach'}