Last active
August 29, 2015 14:22
-
-
Save loic/6f451a411f7186994709 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Yup
F('data')['owner']['birthday'].cast(DateField).year() < 1980
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
Book.objects.filter(F('data').key('owner').key('birthday').cast(DateField).year() < 1980)
?