Skip to the page

Django and DRF · mid

DRF serializers, permissions and building an endpoint

A serializer turns objects into plain data on the way out and validates input on the way in. Object permissions run only for single-object actions, so list endpoints must scope the queryset. A good endpoint uses a service function, correct status codes and race-safe updates.

The notes

What you must know

  • Out: to_representation turns an instance into plain Python types and a renderer makes JSON. In: is_valid() runs field conversion and validators, then validate_<field>(), then validate(attrs). is_valid(raise_exception=True) gives a 400.
  • save() calls create(validated_data) with no instance and update(instance, validated_data) with one. Extra keyword arguments such as save(owner=user) are merged. partial=True is for PATCH.
  • Writable nested serializers need your own create() and update(). List fields explicitly instead of fields = "all", which lets a client send owner (mass assignment). Mark fields read-only.
  • DRF runs object permissions only in get_object() (retrieve, update, destroy). For list they are not checked, and not on create. Scope the data in get_queryset(): Invoice.objects.filter(owner=request.user). Other people's objects are then 404.
  • A custom action that uses Model.objects.get() skips object permissions; use self.get_object() or check_object_permissions.
  • Coupon endpoint: input serializer, cart looked up for request.user, rules in a service function, timezone.now() for expiry, a conditional update with F() for the usage limit inside atomic(), a UniqueConstraint for one use per user with IntegrityError caught in a nested atomic block.
  • Status codes: 400 bad input, 404 unknown coupon or cart, 409 already used or limit reached, 200 with the new total. Money in integer paise or Decimal. Add IsAuthenticated, a throttle, tests for each rule and a concurrency test.
  • Serializers are slow per object for big exports; use values() or values_list() with a streamed response, or a background job.

The red pen

Common traps: mistake → fix

  • Mistake: IsOwner on the viewset protects the list endpoint

    Fix: object permissions do not run for list.

  • Mistake: fields = "all" is convenient and safe

    Fix: it exposes and accepts every field.

  • Mistake: A 403 for someone else's invoice is the best answer

    Fix: 404 hides that it exists.

  • Mistake: Check the usage limit in Python, then save

    Fix: two requests can pass together; use a conditional update.

Easy to hard

Questions interviewers ask, easy to hard

  1. What does a serializer do on the way in and on the way out?
  2. What happens between is_valid() and save()?
  3. Why can any user list every invoice when detail returns 403?
  4. How do you fix mass assignment in a ModelSerializer?
  5. Build POST /api/coupons/apply/ with limits and one use per user.

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview