Last active
September 30, 2015 17:29
-
-
Save kezabelle/07e6846291873a9672b6 to your computer and use it in GitHub Desktop.
Generating a Q object for ensuring maximal depth for onetoones.
This file contains 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
LOOKUP_SEP = '__' # fake it. | |
def Q(**kws): # fake it. | |
return kws | |
def make(relation_str, sep=LOOKUP_SEP): | |
""" | |
Given something like: | |
'a__b__c__d__e' | |
return a Q object composed of: | |
{ | |
'a__isnull': False, | |
'a__b__isnull': False, | |
'a__b__c__isnull': False, | |
'a__b__c__d__isnull': False, | |
'a__b__c__d__e__isnull': False | |
} | |
all AND'd together. | |
""" | |
template = '{val!s}__isnull' | |
if LOOKUP_SEP not in relation_str: | |
to_q = {template.format(val=relation_str): False} | |
else: | |
relations = relation_str.split(LOOKUP_SEP) | |
relations_count = len(relations) | |
def sliceup(iterable, length): | |
return LOOKUP_SEP.join(iterable[0:length+1]) | |
to_q = { | |
template.format(val=sliceup(relations, x)): False | |
for x in range(relations_count) | |
} | |
return Q(**to_q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment