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

Add shortcut for alias in specs #41

Closed j4mie closed 3 years ago

j4mie commented 3 years ago

This introduces a new bit of syntax into a spec, providing a shortcut to alias the output of a projector. This brings things a little closer to the syntax used by SSM plugins, where the output key name is clearly visible in the spec.

This allows you to do things like this:

def project_full_name(instance):
    return {"full_name": f"{instance.first_name} {instance.last_name}"}

full_name = (qs.include_fields("first_name", "last_name"), project_full_name)

spec = [
    "name",
    {"whole_entire_name": full_name},
    {"age_in_years": "years_since_birth"},
]
j4mie commented 3 years ago

Superseded by #43