Skip to content

Instantly share code, notes, and snippets.

@thorin-schiffer
Created December 31, 2021 15:20
Show Gist options
  • Save thorin-schiffer/96227c1ad06f67c54251bc804ac635da to your computer and use it in GitHub Desktop.
Save thorin-schiffer/96227c1ad06f67c54251bc804ac635da to your computer and use it in GitHub Desktop.
Django-plpy custom ORM lookups
from django_plpy.installer import plfunction
from django.db.models import Transform
from django.db.models import IntegerField
from tests.books.models import Book
@plfunction
def plsquare(a: int) -> int:
return a * a
class PySquare(Transform):
lookup_name = "plsquare"
function = "plsquare"
IntegerField.register_lookup(PySquare)
assert Book.objects.filter(amount_stock__plsquare=400).exists()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment