Opened 7 years ago

Closed 6 years ago

#29385 closed Cleanup/optimization (fixed)

Make ModelDetailView show model properties

Reported by: Bostjan Kaluza Owned by: humbertotm
Component: contrib.admindocs Version: 2.0
Severity: Normal Keywords:
Cc: Nat S Dunn Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Model properties (like one below) do not show in the admin documentation:

  class MyModel(models.Model):
      name = models.CharField(max_length=200)
      public = models.BooleanField(default=False)
      date_approved = models.DateTimeField(null=True, blank=True)

      @property
      def status(self):
      """
        returns the status of object
      """
      if self.date_approved and self.public:
        return "Public"
      elif self.public:
        return "Pending Approval"
      else:
        return "Private"

Change History (12)

comment:1 by Bostjan Kaluza, 7 years ago

It seem easy to fix. In file django/contrib/admindocs/views.py method ModelDetailView.get_context_data gathers model methods, but skips the properties as they are not methods. So we need to (1) add properties to the condition and (2) append the property to the list of fields.

# Gather model methods.
for func_name, func in model.__dict__.items():
    #
    # CHANGE 1: add isinstance(func, property) to the condition
    #
    # if inspect.isfunction(func): # old line
    if inspect.isfunction(func) or isinstance(func, property):
        try:
            for exclude in MODEL_METHODS_EXCLUDE:
                if func_name.startswith(exclude):
                    raise StopIteration
        except StopIteration:
            continue
        verbose = func.__doc__
        if verbose:
            verbose = utils.parse_rst(utils.trim_docstring(verbose), 'model', _('model:') + opts.model_name)

        #
        # CHANGE 2: If this is a property, show it as a 'field'
        #
        if isinstance(func, property):
            fields.append({
                'name': func_name,
                'data_type': get_return_data_type(func_name),
                'verbose': verbose or '',
            })
        # Else if a method has no arguments, show it as a 'field', otherwise
        # as a 'method with arguments'.
        elif func_has_no_args(func) and not func_accepts_kwargs(func) and not func_accepts_var_args(func):
            fields.append({
                'name': func_name,
                'data_type': get_return_data_type(func_name),
                'verbose': verbose or '',
            })
        else:
            arguments = get_func_full_args(func)
            # Join arguments with ', ' and in case of default value,
            # join it with '='. Use repr() so that strings will be
            # correctly displayed.
            print_arguments = ', '.join([
                '='.join(list(arg_el[:1]) + [repr(el) for el in arg_el[1:]])
                for arg_el in arguments
            ])
            methods.append({
                'name': func_name,
                'arguments': print_arguments,
                'verbose': verbose or '',
            })

comment:3 by Tim Graham, 7 years ago

Has patch: set
Patch needs improvement: set
Summary: Admindocs doesn't show model propertiesMake ModelDetailView show model properties
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

A test and documentation update are also required.

comment:4 by Nat S Dunn, 7 years ago

Cc: Nat S Dunn added

comment:5 by humbertotm, 6 years ago

Owner: changed from nobody to humbertotm
Status: newassigned

I'd be glad to work on the required test and documentation.

comment:6 by humbertotm, 6 years ago

Pull request: https://github.com/django/django/pull/10004

Building on the fix provided by PR #9929, a unit test for the enhancement has been added. Additional documentation has not been added as the fix seems to fall within what is covered by The Django admin documentation generator. I will be glad to continue documenting with a bit of orientation.

Let me know if you have any comments regarding the unit test.

comment:7 by Claude Paroz, 6 years ago

For the docs, something like with all the fields and methods available on it -> with all the fields, properties and methods available on it, with a versionchanged section stating that showing model properties has been added.

in reply to:  7 ; comment:8 by humbertotm, 6 years ago

Replying to Claude Paroz:

For the docs, something like with all the fields and methods available on it -> with all the fields, properties and methods available on it, with a versionchanged section stating that showing model properties has been added.

Great! I'm on it.
Thanks!

in reply to:  8 comment:9 by humbertotm, 6 years ago

Added documentation to PR. Let me know what you think, and how this can be improved.
Thanks!

comment:10 by Claude Paroz, 6 years ago

Patch needs improvement: unset

comment:11 by Carlton Gibson, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:12 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 747ff7a3:

Fixed #29385 -- Made admindocs ModelDetailView show model properties.

Original patch by bkaluza. Tests and docs by humbertotm.

Note: See TracTickets for help on using tickets.
Back to Top