- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个用例,我需要检索 Django 模型管理 ListView 中每一行的状态信息。
我可以使用如下代码检索数据:
def blah(admin.ModelAdmin):
@staticmethod
def status(instance):
return Blah(instance).get_info()['status']
readonly_fields = ('id', 'status')
但是,这个“Blah”类会同时返回状态和进度。有没有一种简单的方法可以用实例调用这个“Blah”类,返回状态字段和进度字段,并将两者添加到 readonly_fields 元组而不重复,如:
def blah(admin.ModelAdmin):
@staticmethod
def status(instance):
return Blah(instance).get_info()['status']
@staticmethod
def progress(instance):
return Blah(instance).get_info()['progress']
readonly_fields = ('id', 'status', 'progress')
最佳答案
我想你可以使用类装饰器。
def get_blah_info(field):
return staticmethod(lambda x: Blah(x).get_info()[field])
def blah_decorator(*fields):
def wrapper(cls):
for field in fields:
setattr(cls, field, get_blah_info(field))
cls.readonly_fields.append(field)
return cls
return wrapper
@blah_decorator('status', 'progress')
class BlahAdmin(admin.ModelAdmin):
readonly_fields = ['id']
但我不明白你为什么要使用静态方法。
一个更高级的例子:
from django.utils.translation import ugettext_lazy as _
def get_blah_info(blah_class, field):
def get_info(self, instance):
return blah_class(instance).get_info()[field]
return get_info
def blah_decorator(blah_class, **fields):
def wrapper(cls):
# Make sure readonly_fields is a list so that we can append elements
readonly_fields = getattr(cls, 'readonly_fields', [])
if not hasattr(readonly_fields, 'append'):
readonly_fields = list(readonly_fields)
for field, short_description in fields.items():
# Define the method for each field and append it to readonly_fields
get_info = get_blah_info(blah_class, field)
get_info.__name__ = field
get_info.short_description = short_description
setattr(cls, field, get_info)
readonly_fields.append(field)
cls.readonly_fields = readonly_fields
return cls
return wrapper
@blah_decorator(Blah, status=_("Status"), progress=_("Progress"))
class BlahAdmin(admin.ModelAdmin):
readonly_fields = ['id']
当然,如果您愿意,可以将上面的示例修改为使用静态方法。
另一种解决方案是使用 metaclass .
class BlahMetaclass(type):
@staticmethod
def get_blah_info(blah_class, field):
def get_info(self, instance):
return blah_class(instance).get_info()[field]
return get_info
def __new__(cls, cls_name, bases, attrs):
blah_class = attrs['blah_class']
blah_fields = attrs['blah_fields']
readonly_fields = attrs.get('readonly_fields', [])
if not hasattr(readonly_fields, 'append'):
readonly_fields = list(readonly_fields)
for field, short_description in blah_fields:
if field in attrs:
continue # Let the class have the precedence
get_info = cls.get_blah_info(blah_class, field)
get_info.__name__ = field
get_info.short_description = short_description
attrs[field] = get_info
if field not in readonly_fields:
# Do not add `field` to `readonly_fields` if it is already present.
# This enables to redefine the fields order rather than
# appending `blah_fields`.
readonly_fields.append(readonly_fields)
attrs['readonly_fields'] = readonly_fields
# Optionally remove `blah_class` and `blah_fields` if
# not useful any further.
del attrs['blah_class']
del attrs['blah_fields']
return super().__new__(cls, clsname, bases, attrs)
class BlahModelAdmin(admin.ModelAdmin, metaclass=BlahMetaclass):
"""Optionally, create a new base ModelAdmin."""
class BlahAdmin(BlahModelAdmin):
blah_class = Blah
blah_fields = [
('status' _("Status")),
('progress', _("Progress")),
]
readonly_fields = ['id']
# Or, for instance: readonly_fields = ['status', 'id', 'progress']
# If you want to change the order
关于python - 如何动态地向 Django Model Admin readonly_fields 添加多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38334958/
我只是希望我所有的管理页面都在 /admin 下. 我的主页在 /admin但是当我单击侧栏中的用户时,它会将路径更改为 /users而不是 /admin/users . 我正在使用 rea
我在端口 8011 上设置了一个 Django 服务器,并在端口 80 上有一个 nginx 作为子目录服务。 静态文件,不错。 页面,很好。 但是当我访问/subdirectory/admin/时,
我正在尝试使用 Flask 和 Flask-SuperAdmin 自定义我的 Admin View ,但是,索引 View 和 subview 显然没有使用相同的 is_accessible 方法:
我正在尝试配置我的 .htaccess,以便当有人导航到路径 /wp-admin/admin.php?page=something 时,路径 /wp-admin/something显示在浏览器中,内容
有没有办法整体禁用分页?我希望我的所有记录都显示在一个长列表中,而不是分成几页。我现在正在使用 ListGuesser,我已经尝试过 List 和 ListGuesser,但我无法禁用分页。实际上 r
在本地部署时,我的 RA 项目呈现良好,但是,当我将构建目录复制到 S3 存储桶进行部署时,它呈现的一切都一团糟。有时它有效,但大多数时候它呈现如下图所示。它在带有 AOR 的两种环境中都能正常工作。
我试图在 Datagrid 中显示Field这是多对多关系(中间表)。 我有这些表: 组 - group_subject - 主题 一个小组可以有多个科目 一个主题可以连接多个群组 group_sub
在用于显示用户列表的 Django 管理屏幕中(带有标题,选择要更改的用户),有用户名/电子邮件等字段和员工状态。我想将用户的事件状态添加到该显示中。 尽管大多数 django-admin 自定义问题
无论如何要检查所有 firebase 事务是否在 firebase-admin nodejs 脚本中完成,并正确断开与 firebase 的连接并退出 nodejs 脚本? 目前,即使在所有事务完成后
我正在尝试使用 react-admin 将数据发送到我的自定义 API。我想发送文件,我可以看到有,我想将该数据作为多部分表单数据发送。我遇到过the base64 encoding help pag
我正在尝试在admin部分中实现带有子表单的表单。 form = Fighter() subform = FighterFightHistory() //All of his fights 我的问题如
我在编辑表单中获取记录的“更新”值时遇到了很大的麻烦。我总是得到初始记录值,即使我有一个链接到正确记录源的输入,它应该更新它。 有没有其他方法来获取 SimpleForm 的值? 我有一个简单的编辑表
开发一个不需要翻译的管理员。有没有办法完全关闭它们。就像现在一样,对于诸如通知之类的事情,它们会显示,但我也会收到有关缺少翻译键的控制台警告。 最佳答案 除了 Frederik 的回答之外,以下是禁用
开发一个不需要翻译的管理员。有没有办法完全关闭它们。就像现在一样,对于诸如通知之类的事情,它们会显示,但我也会收到有关缺少翻译键的控制台警告。 最佳答案 除了 Frederik 的回答之外,以下是禁用
我有这样的模型: class User(db.Model): __tablename__ = 'users' __table_args__ = {'mysql_engine' : 'I
我已经使用 Media 类自定义了我的内联模型。 我想自定义内联字段集以实现两件事: 为每一行添加一个按钮 按下“添加新行”时从 javascript 代码运行 我试过覆盖 tabular.html
我的所有管理员/任何 url 正在呈现管理文件,但是当我尝试只点击管理员时,它显示 404 不在这条路线上。我可以为 url/admin 创建单独的路由,但这是他们的任何其他选项。这样我只能通过单一路
虽然我使用了许多图表,如 HighCharts、Charts.js、C3、D3,但是否有人尝试过或是否有一些关于如何将任何图表框架包含在 React-admin 中的文档?我看了一些,但看起来我们必须
react-admin 文档将资源的默认路由解释为: /posts/:id 映射到 PostEdit /posts/:id/show 映射到 PostShow 我希望我的应用程序默认为查看,并且仅当用
我正在使用 react-admin 前端框架,想知道是否有办法从路由中删除“#”。我正在使用 customRoutes 属性为我们的系统提供路由集合。 ``` 最佳答案 您可以创建自己的历
我是一名优秀的程序员,十分优秀!