Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

j4mie
Copy link
Member

@j4mie j4mie commented Jan 29, 2022

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
Copy link
Member Author

j4mie commented Jul 15, 2022

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants