N
N
NyxDeveloper2021-03-03 14:20:28
Django
NyxDeveloper, 2021-03-03 14:20:28

How to get calculated model fields in queryset.values()?

I am implementing a table model that has one calculated field. The tablet must be given in the form of an array of dictionaries through the api. When I try to get a calculated field in queryset.values(), I get an error saying that a field with that name does not exist.
Here is the method in the model:

@property
def summaFakt(self):
 return 111111


Here is getting the list of variables:
list = list.values(
        'id',
        'name',
        'number',
        'sum',    #    злополучное поле
        'description'
    )

When I access the field as an object parameter, everything works as I need:
sum = list.first().sum
But when I try to get the same field in the list, I get an error. What am I doing wrong? How can I achieve the desired result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-03-03
@bacon

summaFakt is what you calculate already at the model level in python, values ​​are fields from the base. Calculate via annotate, although it may turn out that you simply do not need to use values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question