+1 for this approach.

On Sunday, November 19, 2017 at 7:49:09 PM UTC-5, [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()))
>
>
> Ilya.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/4aca72c6-e603-497c-ba0a-c91575d7f808%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to