J
J
JonGalt2017-05-06 13:33:06
Django
JonGalt, 2017-05-06 13:33:06

Complex query in Django?

models.py

class Design(models.Model):
    object = models.ForeignKey(Object)
    company = models.ForeignKey(Company)
    number = models.CharField(max_length=32, verbose_name=u'Шифр проекта')

class Specification(models.Model):
    design = models.ForeignKey(Design)
    name = models.CharField(verbose_name=u'Название спецификации', max_length=32)
    construction_type = models.ForeignKey(ConstructionType, verbose_name=u'Тип конструкций')
    drawings = models.ManyToManyField(Drawing, verbose_name=u'Листы проекта')
    amount = models.FloatField(verbose_name=u'Количество', default=1)
    unit_measure = models.ForeignKey(UnitMeasure, verbose_name=u'Ед. изм.')

class SpecificationMaterial(models.Model):
    specification = models.ForeignKey(Specification)
    material = models.ForeignKey(Material)
    amount = models.FloatField(verbose_name=u'Количество')

class Construction(models.Model):
    object = models.ForeignKey(Object, verbose_name=u'Объект')
    type = models.ForeignKey(ConstructionType, verbose_name=u'Тип конструкции')
    name = models.CharField(max_length=128, verbose_name=u'Название конструкции')

class ConstructionSpecification(models.Model):
    construction = models.ForeignKey(Construction)
    specification = models.ForeignKey(Specification)
    amount = models.FloatField(verbose_name=u'Количество', default=1)

class Material(models.Model):
    group = models.ForeignKey(MaterialGroup)
    code = models.CharField(max_length=48, verbose_name=u'Артикул', blank=True)
    name = models.CharField(max_length=128, verbose_name=u'Название материала')
    unit_measure = models.ForeignKey(UnitMeasure)
    price = models.FloatField(verbose_name=u'Цена', blank=True)

the logic is this:
There is a volume of design documentation (Design), it contains specifications (Specification), they consist of materials (SpecificationMaterial), and there are constructions (Construction). Structures are made from materials that are in the specification, and the design can consist of several specifications.
One specification is compiled for any volume, so it has an amount field (quantity), and the SpecificationConstruction model also has the same field; it indicates which part (or how many parts) of the specification is used in a particular construction.
Calculation of one material:
material_amount = SpecificationConstruction.amount / Specification.amount * SpecificationMaterial.amount
It is necessary to calculate the total number of materials and their cost for the Design object.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question