Skip to content

Commit

Permalink
Add option to display field choices in graph_models (#1854)
Browse files Browse the repository at this point in the history
Use `./manage.py graph_models --display-field-choices` to display the set of
valid choices for a field. This option is not enabled by default, because
choices with a "large" cardinality would mess up the layout, but this can be
very useful for fields with a small number of choices.

Signed-off-by: Titouan Christophe <moiandme@gmail.com>
  • Loading branch information
titouanc committed Feb 7, 2024
1 parent 2fa8feb commit 81e7982
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions django_extensions/management/commands/graph_models.py
Expand Up @@ -80,6 +80,12 @@ def __init__(self, *args, **kwargs):
'dest': 'disable_abstract_fields',
'help': 'Do not show the class member fields that were inherited',
},
'--display-field-choices': {
'action': 'store_true',
'default': False,
'dest': 'display_field_choices',
'help': 'Display choices instead of field type',
},
'--group-models -g': {
'action': 'store_true',
'default': False,
Expand Down
5 changes: 5 additions & 0 deletions django_extensions/management/modelviz.py
Expand Up @@ -102,6 +102,7 @@ def __init__(self, app_labels, **kwargs):
else:
self.app_labels = app_labels
self.rankdir = kwargs.get("rankdir")
self.display_field_choices = kwargs.get("display_field_choices", False)

def generate_graph_data(self):
self.process_apps()
Expand All @@ -124,6 +125,7 @@ def get_graph_data(self, as_json=False):
'cli_options': self.cli_options,
'disable_fields': self.disable_fields,
'disable_abstract_fields': self.disable_abstract_fields,
'display_field_choices': self.display_field_choices,
'use_subgraph': self.use_subgraph,
'rankdir': self.rankdir,
}
Expand Down Expand Up @@ -153,6 +155,9 @@ def add_attributes(self, field, abstract_fields):
t = type(field).__name__
if isinstance(field, (OneToOneField, ForeignKey)):
t += " ({0})".format(field.remote_field.field_name)
if self.display_field_choices and field.choices is not None:
choices = {c for c, _ in field.choices}
t = str(choices)
# TODO: ManyToManyField, GenericRelation

return {
Expand Down

0 comments on commit 81e7982

Please sign in to comment.