Форум сайта python.su
Подскажите модель Material - кол-во материалов, Rashod - израсходаванные материалы
class Material(models.Model):
name = models.CharField(max_length=100)
nomer_materiala = models.CharField(max_length=50)
count = models.DecimalField(max_digits=10, decimal_places=5)
MVZ = models.CharField(max_length=50)
comment = models.TextField ()
obect = models.CharField(max_length=30)
def __unicode__(self):
return '%s %s' % (self.nomer_materiala, self.name)
class Rashod(models.Model):
material = models.ForeignKey(Material)
count = models.DecimalField(max_digits=10, decimal_places=5)
proekt = models.CharField(max_length=30)
ispolnitel = models.ForeignKey(Person)
#building = models.CharField(max_length=30)
SELECT ostatki_Material.Name, Sum(ostatki_Rashod.Count)
FROM ostatki_Rashod INNER JOIN ostatki_Material ON ostatki_Rashod.Material_id = ostatki_Material.ID
GROUP BY ostatki_Material.Name;
select sum(count) from ostatki_rashod where ostatki_rashod.nomer_materiala = nomer_materiala
Rashod.objects.all().count()
Офлайн
Использовать:
- extra: http://docs.djangoproject.com/en/dev/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none
- и недокументированный group_by: http://www.eflorenzano.com/blog/post/secrets-django-orm/
Пока нормальных агрегаций в джанге нет.
Офлайн