Re: Annotations as models fields

2019-06-07 Thread Devender Kumar
Hi, You can use Django annotation feature which will add a field to your queryset objects and that field can behve like any real field On Thu, Jun 6, 2019 at 8:22 PM Chetan Ganji wrote: > Yes Olivier, You are right :P > > If I understand the problem correctly this time, you want the field > ava

Re: Annotations as models fields

2019-06-06 Thread Chetan Ganji
Yes Olivier, You are right :P If I understand the problem correctly this time, you want the field available on django model, but does not want the python to do the calculation. You want the database to compute the value. If yes, you could use a row level trigger i.e after create and after update

Re: Annotations as models fields

2019-06-06 Thread Olivier Dalang
Thanks Chetan for trying to help, but please read carefully the issue, as you didn't understand what I'm trying to do. In the meantime, and for the record, I found this, which works for simple cases. https://github.com/schinckel/django-computed-field On Thu, 6 Jun 2019 at 14:57, Chetan Ganji wr

Re: Annotations as models fields

2019-06-06 Thread Chetan Ganji
I think this would solve your problem. Whatever calculations needs to be done, you can do it manually before saving the Sales instance. Preferably after checking if form.is_valid() in case of django and in the function perform_create() in case of django rest framework. class Sales(models.Model):

Re: Annotations as models fields

2019-06-06 Thread Olivier Dalang
Thanks for the answer. As said, the @property + python method wouldn't work for my use case, as I need to be able to use the field in the queryset to filter/order (plus my actual computed field use aggregates expressions). Cheers, Olivier On Thu, 6 Jun 2019 at 12:36, Chetan Ganji wrote: > I ha

Re: Annotations as models fields

2019-06-06 Thread Chetan Ganji
I had a similar need in one of my clients project, I ended up using @property and a python method. AFAIK, AnnotationField is absent from django models. If you would like to write one, below is the material you need to refer. https://docs.djangoproject.com/en/2.2/howto/custom-model-fields/ Regard

Annotations as models fields

2019-06-06 Thread Olivier Dalang
Dear list, I was wondering whether there's a package or pattern to define annotations as model fields, so that they could really be used as database-side computed fields. Currently, I do something like this (not tested, it's a simplified case): class SalesManager(models.Manager): def get_queryse