Stack Exchange
how to make a property function to act like a real field in the database
i want to make a (group by) which depend on some other fields , 
models.py
class Product(models.Model):
    pass


class Order(models.Model):
    id = models.AutoField(primary_key = True)
    products = models.ManyToManyField(Product ,through='ProductOrder') 
    date_time = models.DateTimeField(default=datetime.now()) 

    @property
    def total(self):
        return self.productorder_set.aggregate(
        price_sum=Sum(F('quantity') * F('product__price'), 
     output_field=IntegerField()) )['price_sum'] 



class ProductOrder(models.Model):
    product = models.ForeignKey(Product, 
    on_delete=models.CASCADE,default='' )
    ordering = models.ForeignKey(Order, 
    on_delete=models.CASCADE,blank=True,null=True)
    quantity = models.IntegerField(default=1)

views.py
class View(ListView):
pass

    def get_context_data(self , *args,**kwargs):
        context = super().get_context_data(*args , **kwargs)
        context['daily'] = 
    Order.objects.values('date_time').aggregate(days=Sum('total'))
    return context

(Cannot resolve keyword 'total' into field. Choices are:...)

is it possible to make the property function act as a field in the db ? or if there another way to achieve the same thing i trying to implement ? 
i also tried to 
views.py 
from .models.Order import total 

context['daily'] = 
Order.objects.values('date_time').aggregate(days=Sum(total()))

but doesnt work ! 
thanks for any advice ..

You can annotate your queryset. For example:
Order.objects.annotate(
    _total=Sum(F('productorder__quantity') * F('productorder__product__price'))
)
You can then for example annotate per date_time value, like:
Order.objects.values('date_time').annotate(
    _total=Sum(F('productorder__quantity') * F('productorder__product__price'))
).order_by('date_time')
If you want to do this per day, you can first make an annotation that extracts the day:
from django.db.models.functions import TruncDay

Order.objects.annotate(
    day=TruncDay('date_time')
).values('day').annotate(
    _total=Sum(F('productorder__quantity') * F('productorder__product__price'))
).order_by('day')
This is a QuerySet of dictionaries, that looks like:
<QuerySet [
    {'day': datetime(2019, 1, 1), '_total': 123},
    {'day': datetime(2019, 1, 2), '_total': 456},
    {'day': datetime(2019, 1, 3), '_total': 789},
]>


thanks alot , thats what i tried to achieve , you saved my day , much appreciate

sorry , one question , should i update my old application which builds with old versions of django (no longer django support) to new versions? thanks

@namo: that is usually a good idea, since new security vulnerabilities can be found and are not patched in non-supported versions. You will need to look into the release notes however (for example https://docs.djangoproject.com/en/2.2/releases/2.2/#backwards-incompatible-changes-in-2-2) what functionalities have changed between two Django versions.
