django-readers
django-readers copied to clipboard
Producer and projector for grabbing the value of a related field
This generalises the pk_list functions (which were added specially to sort-of replace a feature that existed in SSM) to allow fetching the value or values of any field from a related object or objects.
Example:
prepare, project = specs.process([
"title",
{"author_names": pairs.related_field("author_set", "name")},
{"publisher_name": pairs.related_field("publisher", "name")},
])
queryset = prepare(Book.objects.all())
result = project(queryset.first())
{
"title": "django-readers for dummies",
"author_names": ["Jamie", "PMG"],
"publisher_name": "DabApps Press"
}
The existing pk_list functions have been refactored to use related_field.
Concern about this is that it doesn't compose nicely. This won't work:
prepare, project = specs.process([
"title",
{"publisher_id": pairs.related_field("publisher", "id")},
{"publisher_name": pairs.related_field("publisher", "name")},
])
You'd need to do this as a projector pair instead to avoid trying to prefetch the same queryset twice. Needs more thought.
I wonder if this would serve the use-case I have in #97 🤔