[Fixed]-Sphinx – Documenting Django models


You have to use

@param description

in the model’s docstring, for each field you want to be documented by sphinx.

Or in alternative, you should take a look at this snippet that does what you want (minus the boring writing part)


If you plan to use this snippet, in django>=1.6


has been removed please change it to



Here is a snippet mentioned by @Samuele Mattiuzzo above, updated to support actual Django versions (tested on 1.11 LTS):

import inspect
from django.utils.html import strip_tags

def process_docstring(app, what, name, obj, options, lines):
    # This causes import errors if left outside the function
    from django.db import models

    # Only look at objects that inherit from Django's base model class
    if inspect.isclass(obj) and issubclass(obj, models.Model):
        # Grab the field list from the meta class
        fields = obj._meta.fields

        for field in fields:
            # Decode and strip any html out of the field's help text
            help_text = strip_tags(field.help_text)

            # Decode and capitalize the verbose name, for use if there isn't
            # any help text
            verbose_name = field.verbose_name

            if help_text:
                # Add the model field to the end of the docstring as a param
                # using the help text as the description
                lines.append(u':param %s: %s' % (field.attname, help_text))
                # Add the model field to the end of the docstring as a param
                # using the verbose name as the description
                lines.append(u':param %s: %s' % (field.attname, verbose_name))

            # Add the field's type to the docstring
            lines.append(u':type %s: %s' % (field.attname, type(field).__name__))

    # Return the extended docstring
    return lines

def setup(app):
    # Register the docstring processor with sphinx
    app.connect('autodoc-process-docstring', process_docstring)

Just add it to the end of your conf.py and model fields will be added automatically to documentation.


If you plan to use this snippet, in django 1.7+:

  1. You had to setup your django project

  2. Use


    instead of:



What you easily could do is add a docstring below each field.
Sphinx will recognize this and render a description for each field.

class Person(models.Model):
    """The model's docstring"""

    first_name = models.CharField(max_length=23)
    """This is the first name pf the person"""

    last_name = models.CharField(max_length=23)
    "This is the last name pf the person"
    # even single-quoted strings work.

This is easier as the other approach and keeps the documentation where it should be: at the target.


Leave a comment