Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Last active September 30, 2015 17:29
Show Gist options
  • Save kezabelle/07e6846291873a9672b6 to your computer and use it in GitHub Desktop.
Save kezabelle/07e6846291873a9672b6 to your computer and use it in GitHub Desktop.
Generating a Q object for ensuring maximal depth for onetoones.
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