Reputation: 2635
If I have a Django model with a property defined as
def __task_state(self):
if self.task_id is None:
return None
else:
return # Calculate state somehow
__task_state.short_description = _("Task State")
# Task state
task_state = property(__task_state)
Now how do I access short_description for the property?
I am trying to iterate over all properties and field for the model so I can get verbose_name equivalent to use in detailed view and for list view column header.
I can't use __task_state.short_description
directly and I can't figure out how to get this info using task_state
doc() function says not defined, short_description
is obviously not going to be an attribute of property.
Can't seem to find anything on it anywhere.
Everywhere all it says is use short_description for property text but no one mentions how to access it.
This is how I am trying to find all properties for my model in case anyone feel its relevant
# Get property (name, value) set for the object
def get_properties(self):
# Get all properties
property_names = [
name
for name in dir(Job)
if isinstance(getattr(Job, name), property)
]
# Return list
result = []
# Let's prune out pk from the result as we don't want it
for name in property_names:
if name == "pk":
continue
# TODO: Get short_description for the property somehow
# Have property name str for the first value, ***want*** detailed name for the second one and have property value for the third one here
result.append((name, **name**, getattr(self, name)))
return result
Upvotes: 0
Views: 680
Reputation: 21787
Instances of property
have their getter method stored as fget
, also you can store their documentation by passing a keyword argument doc
(to the property
decorator) or writing it on the getter method. Instead of accessing its documentation by the instance try using the class:
def get_properties(self):
# Get all properties
property_names = [
name
for name in dir(Job)
if isinstance(getattr(Job, name), property)
]
# Return list
result = []
# Let's prune out pk from the result as we don't want it
for name in property_names:
if name == "pk":
continue
short_description = getattr(Job, name).fget.short_description
# short_description = getattr(Job, name).__doc__
result.append((name, short_description, getattr(self, name)))
return result
Upvotes: 2