99from django .utils .module_loading import import_string
1010from sphinx .util .docstrings import prepare_docstring
1111
12- from .config import CHOICES_LIMIT
1312from .field_utils import get_field_type , get_field_verbose_name
1413
1514FIELD_DESCRIPTORS = (FileDescriptor , related_descriptors .ForwardManyToOneDescriptor )
2322 PhoneNumberDescriptor = None
2423
2524
26- def improve_attribute_docstring (attribute , name , lines ):
25+ def improve_attribute_docstring (app , attribute , name , lines ):
2726 """
2827 Improve the documentation of various model fields.
2928
3029 This improves the navigation between related objects.
3130
31+ :param app: The Sphinx application object
32+ :type app: ~sphinx.application.Sphinx
33+
3234 :param attribute: The instance of the object to document
3335 :type attribute: object
3436
@@ -56,21 +58,21 @@ def improve_attribute_docstring(attribute, name, lines):
5658 f"Internal field, use :class:`~{ cls_path } .{ field .name } ` instead."
5759 )
5860 else :
59- lines .extend (get_field_details (field ))
61+ lines .extend (get_field_details (app , field ))
6062 elif isinstance (attribute , FIELD_DESCRIPTORS ):
6163 # Display a reasonable output for forward descriptors (foreign key and one to one fields).
62- lines .extend (get_field_details (attribute .field ))
64+ lines .extend (get_field_details (app , attribute .field ))
6365 elif isinstance (attribute , related_descriptors .ManyToManyDescriptor ):
6466 # Check this case first since ManyToManyDescriptor inherits from ReverseManyToOneDescriptor
6567 # This descriptor is used for both forward and reverse relationships
6668 if attribute .reverse :
67- lines .extend (get_field_details (attribute .rel ))
69+ lines .extend (get_field_details (app , attribute .rel ))
6870 else :
69- lines .extend (get_field_details (attribute .field ))
71+ lines .extend (get_field_details (app , attribute .field ))
7072 elif isinstance (attribute , related_descriptors .ReverseManyToOneDescriptor ):
71- lines .extend (get_field_details (attribute .rel ))
73+ lines .extend (get_field_details (app , attribute .rel ))
7274 elif isinstance (attribute , related_descriptors .ReverseOneToOneDescriptor ):
73- lines .extend (get_field_details (attribute .related ))
75+ lines .extend (get_field_details (app , attribute .related ))
7476 elif isinstance (attribute , (models .Manager , ManagerDescriptor )):
7577 # Somehow the 'objects' manager doesn't pass through the docstrings.
7678 module , model_name , field_name = name .rsplit ("." , 2 )
@@ -92,17 +94,22 @@ def improve_attribute_docstring(attribute, name, lines):
9294 lines .extend (docstring_lines [:- 1 ])
9395
9496
95- def get_field_details (field ):
97+ def get_field_details (app , field ):
9698 """
9799 This function returns the detail docstring of a model field.
98100 It includes the field type and the verbose name of the field.
99101
102+ :param app: The Sphinx application object
103+ :type app: ~sphinx.application.Sphinx
104+
100105 :param field: The field
101106 :type field: ~django.db.models.Field
102107
103108 :return: The field details as list of strings
104109 :rtype: list [ str ]
105110 """
111+ choices_limit = app .config .django_choices_to_show
112+
106113 field_details = [
107114 f"Type: { get_field_type (field )} " ,
108115 "" ,
@@ -111,13 +118,16 @@ def get_field_details(field):
111118 if hasattr (field , "choices" ) and field .choices :
112119 field_details .extend (["" , "Choices:" , "" ])
113120 field_details .extend (
114- [f"* ``{ key } ``" for key , value in field .choices [:CHOICES_LIMIT ]]
121+ [
122+ f"* ``{ key } ``" if key != "" else "* ``''`` (Empty string)"
123+ for key , value in field .choices [:choices_limit ]
124+ ]
115125 )
116126 # Check if list has been truncated
117- if len (field .choices ) > CHOICES_LIMIT :
127+ if len (field .choices ) > choices_limit :
118128 # If only one element has been truncated, just list it as well
119- if len (field .choices ) == CHOICES_LIMIT + 1 :
129+ if len (field .choices ) == choices_limit + 1 :
120130 field_details .append (f"* ``{ field .choices [- 1 ][0 ]} ``" )
121131 else :
122- field_details .append (f"* and { len (field .choices ) - CHOICES_LIMIT } more" )
132+ field_details .append (f"* and { len (field .choices ) - choices_limit } more" )
123133 return field_details
0 commit comments