7π
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)
[EDIT]
If you plan to use this snippet, in django>=1.6
obj._meta._fields()
has been removed please change it to
_meta.fields
3π
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))
else:
# 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.
- What does related_name do?
- Instantiate model instance with manytomany field in Django
- (gcloud.app.deploy) Error Response: [7] Access Not Configured. Cloud Build has not been used in project
- Validation using DeleteView before deleting instance
- Django β change field validation message
1π
If you plan to use this snippet, in django 1.7+:
- You had to setup your django project
django.setup()
- Use
obj._meta.get_fields()
instead of:
obj._meta._fields()
- Using User.objects.get_or_create() gives invalid password format in django?
- How to clear all session variables without getting logged out
1π
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.
- Django: authenticating against remote LDAP user β simple example?
- How do I check if a given datetime object is "between" two datetimes?
- How to connect Django in EC2 to a Postgres database in RDS?
- Django: define a name for reverse ForeignKey