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/b659a3a8-40a0-4863-ae00-38d3c2c13c22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.