Skip to content

Instantly share code, notes, and snippets.

@loic
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loic/6f451a411f7186994709 to your computer and use it in GitHub Desktop.
Save loic/6f451a411f7186994709 to your computer and use it in GitHub Desktop.
class F(Combinable):
def __init__(self, name):
self.name = name
self.functions = []
def __getattr__(self, name):
def wrapper(*args, **kwargs):
self.functions.append((name, args, kwargs))
return self
return wrapper
Book.objects.filter(F('data').key('owner').key('name') == 'Brad')
Book.objects.filter(F('data').key('owner').key('name').eq('Brad'))
Book.objects.filter(F('data').key('owner').key('birthday').cast(DateField).year() < 1980)
Book.objects.filter(F('data').key('owner').key('birthday').cast(DateField).year().lt(1980))
# Allow transforms in order_by:
https://code.djangoproject.com/ticket/24747
# Allow Func Expressions to be used as Transforms:
https://code.djangoproject.com/ticket/24629
Marc's as_transform() PR
https://github.com/django/django/pull/4748
Josh's PR
https://github.com/django/django/pull/4482
@patrys
Copy link

patrys commented Jun 4, 2015

Book.objects.filter(F('data').key('owner').key('birthday').cast(DateField).year() < 1980)?

@loic
Copy link
Author

loic commented Jun 4, 2015

Yup

@patrys
Copy link

patrys commented Jun 4, 2015

F('data')['owner']['birthday'].cast(DateField).year() < 1980

@patrys
Copy link

patrys commented Jun 4, 2015

And the example from yesterday:

book = M(Book)
Book.objects.filter(
    book.data['owner']['birthday'].cast(DateField).year() < 1980,
    book.title.lower().startswith('a'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment