On Monday 20 November 2017 02:49:09 [email protected] wrote: > Ok, can we have > > is_adult = models.DBCalculatedField(ExpressionWrapper(Q(age__gt=18), > output_field=BooleanField())) > > Any query for model with this field will be automatically annotated with > .annotate(is_adult =ExpressionWrapper(Q(age__gt=18), > output_field=BooleanField())) > > We may use F fields also, something like > full_name = models.DBCalculatedField(ExpressionWrapper(F('first_name') + > F('last_name'), output_field=CharField())) > > We may simplify it to > is_adult = models.DBCalculatedField(expr=Q(age__gt=18), > output_field=BooleanField())) > > It will work for any object, created from database. > > For local computation we may give user ability to provide lambda or > decorate method (as in your example). > > After it, I can create my own mixin as *third party lib* to patch model > with DBCalculatedField and calcuate its expression locally. > > > class Customer(models.Model, UnsafeCalculateDbLocallyMixin): > age = models.IntegerField() > is_adult = models.DBCalculatedField(expr=Q(age__gt=18), > output_field=BooleanField())) >
I will be perfectly happy with that.
