Fixed #30271 -- Added the Sign database function.

This commit is contained in:
Nick Pope 2019-03-20 08:27:34 +00:00 committed by Mariusz Felisiak
parent 5935a9aead
commit d26b242443
No known key found for this signature in database
GPG key ID: 2EF56372BA48CD1B
6 changed files with 88 additions and 2 deletions

View file

@ -1099,6 +1099,31 @@ It can also be registered as a transform. For example::
>>> # Get vectors whose round() is less than 20
>>> vectors = Vector.objects.filter(x__round__lt=20, y__round__lt=20)
``Sign``
--------
.. class:: Sign(expression, **extra)
.. versionadded:: 3.0
Returns the sign (-1, 0, 1) of a numeric field or expression.
Usage example::
>>> from django.db.models.functions import Sign
>>> Vector.objects.create(x=5.4, y=-2.3)
>>> vector = Vector.objects.annotate(x_sign=Sign('x'), y_sign=Sign('y')).get()
>>> vector.x_sign, vector.y_sign
(1, -1)
It can also be registered as a transform. For example::
>>> from django.db.models import FloatField
>>> from django.db.models.functions import Sign
>>> FloatField.register_lookup(Sign)
>>> # Get vectors whose signs of components are less than 0.
>>> vectors = Vector.objects.filter(x__sign__lt=0, y__sign__lt=0)
``Sin``
-------