Is it possible to create a QuerySet from a model and determine in the sample a field with all values equal to "0.0" without losing the opportunity to work with the query object and its fields as with a QuerySet? After combining, I want to make a "UNION ALL" with a different sample and calculate the sum of the fields 'plan' and 'fact'.
What other solutions besides the RAW request should be considered?
from django.db.models import Model, ForeignKey, TextField, DecimalField, DateField, CASCADE class ModelValueMixin(Model): date = DateField(auto_now=True) value = DecimalField() classifier = ForeignKey('Catalog', on_delete=CASCADE) class Meta: abstract = True class ModelValue1(ModelValueMixin): pass class ModelValue2(ModelValueMixin): pass class Catalog(Model): code = TextField() name = TextField() An example of how I see it with SQLAlchemy:
session.query( ModelValues1.value.label('plan'), literal_column('0').label('fact'), ModelCatalog.code.label('code'), ModelCatalog.name.label('name') ).select_from(ModelValues1).outerjoin(ModelCatalog) And directly in SQL, I see it like this:
SELECT "catalog"."id", "catalog"."code", "catalog"."full_name", "values1"."value" AS "plan", (0) AS "fact" FROM "catalog" LEFT OUTER JOIN "values1" ON (catalog."id" = "values1"."catalog_id") The final goal is to get a summary table with the fields "code", "name", "sum_plan", "sum_fact."
ModelValues1andModelCatalog. And better tell in more detail what result you want to achieve. And that is the persistent feeling that you reinvent the wheel. - Sergey Gornostaev