F
F
fatalixan2020-06-19 10:23:37
Django
fatalixan, 2020-06-19 10:23:37

How to combine a queryset with a calculated field (which is created based on the same queryset)?

Model manager

class DairyManager(models.Manager): 
    def get_queryset(self):
        return super(DairyManager, self).get_queryset().extra(select={'totalsum':'count*cost'})


Model

class Dairy(models.Model):
...
    count = models.IntegerField("Количество")
    cost = models.FloatField('Цена')
...
    total = DairyManager()
    objects = models.Manager()

How do I combine these qs1 and qs2 to end up with one table???
view
qs1 = Dairy.total.all()
qs2 = Dairy.objects.all()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jack444, 2020-06-19
@Jack444

The idea is that it might work

qs1.union(qs2)

The documentation says that union is used for merging, but in fact this does not always work for some reason. An error may come out saying qs1 does not have a union attribute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question