Python 3.x 如何访问Django中属性的简短描述

Python 3.x 如何访问Django中属性的简短描述,python-3.x,django,django-models,Python 3.x,Django,Django Models,如果我有一个Django模型,其属性定义为 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_s

如果我有一个Django模型,其属性定义为

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)
现在如何访问该属性的简短描述

我试图迭代模型的所有属性和字段,以便获得详细视图和列表视图列标题中使用的详细名称

我不能直接使用
\u任务\u状态。简短的描述
,我不知道如何使用
任务\u状态
doc()函数表示未定义,
short\u description
显然不是属性的属性。 好像什么地方都找不到。 它说的所有地方都是对属性文本使用简短的描述,但没有人提到如何访问它

这就是我如何试图找到我的模型的所有属性,以防有人觉得它相关

    # 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

property
实例的getter方法存储为
fget
,您还可以通过传递关键字参数
doc
(传递到
属性
装饰器)或将其写入getter方法来存储文档。请尝试使用类,而不是通过实例访问其文档:

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

property
实例的getter方法存储为
fget
,您还可以通过传递关键字参数
doc
(传递到
属性
装饰器)或将其写入getter方法来存储文档。请尝试使用类,而不是通过实例访问其文档:

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

非常感谢你
getattr(Job,name).fget.short\u description
非常有效,但是getattr(Job,name)。\uu doc\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。当我将属性描述更改为
task\u state=property(\u\u task\u state,doc=\u task\u state.short\u description)
doc按照您的建议工作!这就是您建议的编写方式吗?@LLawliet如果您在
\uuu task\u state
(fget/getter)方法中添加docstring(方法开头的三重引号字符串),它将自动用作属性的docstring。非常感谢
getattr(Job,name).fget.short\u description
非常有效,但是getattr(Job,name)。\uu doc\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。当我将属性描述更改为
task\u state=property(\u\u task\u state,doc=\u task\u state.short\u description)
doc按照您的建议工作!这就是您建议的编写方式吗?@LLawliet如果您在
\uuu task\u state
(fget/getter)方法中添加docstring(方法开头的三重引号字符串),它将自动用作属性的docstring