Created
November 15, 2012 04:37
-
-
Save joshourisman/4076677 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
for Model, relations in RELATIONSHIPS.items(): | |
fields = [relations[relation]['field'] \ | |
for relation in relations \ | |
if relations[relation]['model'] is Constituent] | |
relationship_fields = {field: '{}_constituent_id'.format(field) \ | |
for field in fields} | |
or_filters = [Q(**{'{}__isnull'.format(field): True}) \ | |
for field in fields] | |
null_relations = reduce(lambda x, y: x | y, or_filters) | |
filters = null_relations | |
if constituent_id is not None: | |
id_filters = [Q(**{field: constituent_id}) \ | |
for field in relationship_fields.values()] | |
ids = reduce(lambda x, y: x & y, id_filters) | |
filters = null_relations & ids | |
objects = Model.objects.filter(filters) | |
yield objects, relationship_fields |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment