Skip to content

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.

4 participants