What this prompt does
This prompt makes the AI build a complete Django REST Framework API for a resource, structured the way you'd build one for production. You name the [resource_name], the [project_type], and the [model_fields], and the model generates the Django model in [app_name]/models.py with proper field types, validators, a Meta class (ordering, indexes, verbose_name), a str, and any [model_methods], then the serializers, viewset, permissions, filtering, pagination, URLs, and OpenAPI docs.
The structure works because it splits a lean list serializer from a full detail serializer and wires eager loading from day one. The list serializer carries minimal fields for collections; the detail serializer includes [nested_relations] and uses SerializerMethodField for [computed_fields]. The ModelViewSet uses select_related/prefetch_related for [eager_loaded_relations] to prevent N+1, applies [permission_scheme], adds django-filter on [filter_fields] and [search_fields], paginates with [pagination_style] at [page_size], and documents everything with drf-spectacular. Wiring the schema early means the frontend never guesses the contract.
When to use it
- You need a clean, documented DRF backend for a SaaS or app feature quickly.
- You want list and detail serializers split so collection endpoints stay lean.
- You need select_related/prefetch_related in place from the start to avoid N+1 queries.
- You have an object-level permission scheme (public read, auth to create, ownership to edit).
- You want filtering, search, and pagination configured rather than hand-rolled.
- You want an OpenAPI schema generated so the frontend team has a firm contract.
Example output
Expect a set of code blocks: the model with Meta and methods, a list serializer and a detail serializer (the detail one nesting [nested_relations] and computing [computed_fields]), a ModelViewSet with the eager-loaded queryset and [permission_scheme], a django-filter FilterSet for [filter_fields]/[search_fields], pagination using [pagination_style], a DefaultRouter URL config, and drf-spectacular @extend_schema decorators with request/response examples.
Pro tips
- Keep the list serializer genuinely minimal — pulling
[nested_relations]into collection endpoints is a common cause of slow, oversized list responses. - Match
[eager_loaded_relations]to what each serializer touches: select_related for ForeignKey/OneToOne, prefetch_related for reverse FK and many-to-many. - Implement
[computed_fields]like average_rating with annotations on the queryset where possible, so SerializerMethodField doesn't trigger a query per row. - Choose
[pagination_style]to fit the UI — CursorPagination suits infinite scroll, but it gives up jump-to-page that PageNumberPagination provides. - Make
[permission_scheme]object-level where ownership matters; a blanket IsAuthenticated won't stop a user editing another user's record. - Fill the drf-spectacular examples for real; a schema with empty examples technically documents the API but leaves the frontend guessing at payload shapes.
- Add the Meta indexes the model needs for your
[filter_fields]so the django-filter queries hit an index instead of scanning the table. - Wire
[search_fields]to columns users actually search; a broad search across large text fields without an index degrades as the table grows.