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

Re: annotations only in 1.1 ?

2009-07-31 Thread gentlestone
thx (how stupid I am :-)) On 31. Júl, 14:17 h., Daniel Roseman wrote: > On Jul 31, 12:54 pm, gentlestone wrote: > > > I want find all instances of model KeyWord, where ManyToMany field > > categories is empty. I think in versieon 1.1 is the appropriate code > > like this: > > > ... KeyWord.obj

Re: annotations only in 1.1 ?

2009-07-31 Thread Daniel Roseman
On Jul 31, 12:54 pm, gentlestone wrote: > I want find all instances of model KeyWord, where ManyToMany field > categories is empty. I think in versieon 1.1 is the appropriate code > like this: > > ... KeyWord.objects.annotate(cnt = Count(categories)).filter(cnt = > 0) ... > > What is the similar

Re: Annotations

2009-05-11 Thread Andy Lei
works great, thanks george! does anybody know if this is possible to do directly with the ORM? On May 7, 10:41 pm, George Song wrote: > On 5/7/2009 4:28 PM, Andy Lei wrote: > > > > > I have 3 models.  Something like this: > > > Writer: > >      content = ManyToMany(Content) > > Content > >    

Re: Annotations

2009-05-07 Thread George Song
On 5/7/2009 4:28 PM, Andy Lei wrote: > I have 3 models. Something like this: > > Writer: > content = ManyToMany(Content) > Content > tags = ManyToMany(Tag) > Tag > > Now suppose I have a Tag, call it "tag1". The idea of the query is > that I want the Writers that have the most pieces