Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. from django.db.models import ExpressionWrapper, FloatField, Func
  2.  
  3. class Cast(Func):
  4. function = 'CAST'
  5. template = '%(function)s(%(expressions)s AS %(db_type)s)'
  6. def __init__(self, expression, db_type):
  7. # convert second positional argument to kwarg to be used in function template
  8. super(Cast, self).__init__(expression, db_type=db_type)
  9.  
  10.  
  11. def Round(expr, digits=0, output_field=FloatField()):
  12. # converting to numeric is necessary for postgres
  13. return ExpressionWrapper(Func(Cast(expr, 'numeric'), digits, function='ROUND'), output_field=output_field)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement