dabapps / django-readers

A lightweight function-oriented toolkit for better organisation of business logic and efficient selection and projection of data in Django projects.
https://www.django-readers.org
BSD 2-Clause "Simplified" License
186 stars 7 forks source link

Producer and projector for grabbing the value of a related field #52

Open j4mie opened 2 years ago

j4mie commented 2 years ago

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.

j4mie commented 2 years ago

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.

carltongibson commented 7 months ago

I wonder if this would serve the use-case I have in #97 🤔