#29385: Admindocs doesn't show model properties
-----------------------------------+--------------------------------------
     Reporter:  bkaluza            |                    Owner:  nobody
         Type:  Bug                |                   Status:  new
    Component:  contrib.admindocs  |                  Version:  2.0
     Severity:  Normal             |               Resolution:
     Keywords:                     |             Triage Stage:  Unreviewed
    Has patch:  0                  |      Needs documentation:  0
  Needs tests:  0                  |  Patch needs improvement:  0
Easy pickings:  0                  |                    UI/UX:  0
-----------------------------------+--------------------------------------

Comment (by bkaluza):

 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.

 {{{#!python
 # 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 '',
             })
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.5d92a01a77f852a9f92473418697ba9f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to