{{{#!python
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"
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29385>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
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>
Comment (by bkaluza):
Pull request: https://github.com/django/django/pull/9929
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:2>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
A test and documentation update are also required.
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:3>
* cc: Nat S Dunn (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:4>
* owner: nobody => humbertotm
* status: new => assigned
Comment:
I'd be glad to work on the required test and documentation.
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:5>
Comment (by humbertotm):
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
[https://docs.djangoproject.com/en/2.0/ref/contrib/admin/admindocs/ 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:6>
Comment (by 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:7>
Comment (by humbertotm):
Replying to [comment:7 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!
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:8>
Comment (by humbertotm):
Added documentation to PR. Let me know what you think, and how this can be
improved.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:9>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:10>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:11>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"747ff7a30b79e12d344169200de24f88a22ddee9" 747ff7a3]:
{{{
#!CommitTicketReference repository=""
revision="747ff7a30b79e12d344169200de24f88a22ddee9"
Fixed #29385 -- Made admindocs ModelDetailView show model properties.
Original patch by bkaluza. Tests and docs by humbertotm.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29385#comment:12>