Skip to main content

Django REST Framework API Builder

Generate a complete DRF API with serializers, viewsets, permissions, filtering, pagination, and OpenAPI documentation for any Django model.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Build a Django REST Framework API for Product in a e-commerce marketplace application. The model has fields: name (CharField), slug (SlugField), description (TextField), price (DecimalField), category (ForeignKey), seller (ForeignKey to User), stock_quantity (IntegerField), is_active (BooleanField), created_at, updated_at. Create: 1) The Django model in products/models.py with proper field types, validators, Meta class (ordering, indexes, verbose_name), and a __str__ method. Include is_in_stock property, apply_discount(percentage), get_average_rating() custom methods. 2) DRF serializers — a list serializer (minimal fields for collection endpoints) and a detail serializer (all fields with nested relations for category (inline), seller (inline with username and rating), reviews (list of recent 5)). Use SerializerMethodField for average_rating, review_count, discounted_price. 3) A ModelViewSet with proper queryset using select_related/prefetch_related for category, seller for select_related; reviews, images for prefetch_related to prevent N+1 queries. 4) Permission classes: list/retrieve is public, create requires authentication, update/delete requires ownership using DRF permissions (IsAuthenticated, IsAdminUser, or custom object-level permissions). 5) Filtering with django-filter: filterable fields category, price range, is_active, seller, search fields name, description, and ordering fields. 6) Pagination using CursorPagination for infinite scroll with configurable page size (default 20). 7) URL configuration using DefaultRouter. 8) API documentation using drf-spectacular with proper @extend_schema decorators including request/response examples.

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.

Frequently Asked Questions

Does this prevent N+1 query problems?
Yes, the ModelViewSet's queryset uses select_related for ForeignKey and OneToOne relations and prefetch_related for reverse FK and many-to-many relations, based on what the serializers access. Wiring eager loading from day one is what keeps the API fast as the related data grows.
Why does it use two serializers instead of one?
It generates a lean list serializer with minimal fields for collection endpoints and a full detail serializer with nested relations for single-record views. Keeping nested relations out of the list serializer prevents slow, oversized responses on endpoints that return many records at once.
What permission model does it set up?
It applies your specified permission scheme using DRF permissions, for example public list and retrieve, authentication required to create, and object-level ownership checks for update and delete. Object-level permissions matter where ownership applies, since a blanket IsAuthenticated would let any user edit another user's record.
Does it generate API documentation?
Yes, it documents the API with drf-spectacular using @extend_schema decorators that include request and response examples. Filling in real examples rather than leaving them empty is what gives the frontend team a firm contract instead of having to guess at payload shapes.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Django & Flask Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support