liveBook · Manning

Field

As explained in Django in Action

Field is a Django Ninja schema-field class that declares field behavior and can provide an external alias for a field name.

Field gives a Django Ninja schema field more control than an ordinary Python type hint. It can supply a default value, change the external field name, or describe how filtering should translate into a Django QuerySet lookup. section 11.5 section 11.2.3

Common uses include:

  • Set an optional filter’s default with Field(None). section 11.2.3
  • Map a friendly API name to an internal Django relationship, such as rooms to room_set. section 11.2.4
  • Map a query parameter to a lookup such as name__istartswith instead of using an exact match. section 11.2.3

Filtering venues

class VenueFilter(FilterSchema): name: Optional[str] = Field(None, q=["name__istartswith"]) The endpoint receives the filter schema through Query(...), then calls filters.filter(venues). A request containing name applies a starts-with lookup to the Venue queryset; an omitted name leaves the filter optional. section 11.2.3

Listing 11.11 Nesting Room data inside serialized Venue objects
# RiffMates/bands/api.py
...
from bands.models import Venue, Room  #1

...

class RoomSchema(ModelSchema): #2
    class Meta:
        model = Room
        fields = ["id", "name"]

class VenueOut(ModelSchema):
    slug: str
    url: str
    rooms: list[RoomSchema] = Field(...,
        alias="room_set")  #3

    ...

Inspect the nested-schema example: rooms is exposed as a friendly API field while alias="room_set" connects it to Django’s related-object manager. section 11.2.4

How Django in Action covers it

How deep it goes
Explains defaults, aliases, lookup mappings, and queryset application through working examples, without detailing Field internals.
Examples
Uses VenueFilter.name with name__istartswith and VenueOut.rooms aliased from Django’s room_set relationship.
What it assumes
Assumes basic Python type hints and Django models while explaining optional fields, ellipsis, Query binding, and related managers in context.
What you can do after
Builds clearer API schemas and applies named Django queryset lookups through typed Ninja filters.
How it connects
Connects Python type hints, Pydantic, FilterSchema, Query, ModelSchema, Django QuerySets, and related-object serialization.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.