如何在 Django 中覆盖超类模型字段的详细名称 [英] how to override the verbose name of a superclass model field in django

查看:21
本文介绍了如何在 Django 中覆盖超类模型字段的详细名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个继承自 SuperFoo 的模型 Foo:

Let's say that I have a model Foo that inherits from SuperFoo:

class SuperFoo(models.Model):
    name = models.CharField('name of SuperFoo instance', max_length=50)
    ...

class Foo(SuperFoo):
    ... # do something that changes verbose_name of name field of SuperFoo

在 Foo 类中,我想覆盖 SuperFoo 的 name 字段的 verbose_name.我可以吗?如果没有,最好的选择是在模型表单定义中设置标签以使其显示在模板中吗?

In class Foo, I'd like to override the verbose_name of the name field of SuperFoo. Can I? If not, is the best option setting a label inside the model form definition to get it displayed in a template?

推荐答案

我使用过的一个简单的 hack 是:

A simple hack I have used is:

class SuperFoo(models.Model):
    name = models.CharField('name of SuperFoo instance', max_length=50)
    ...
    class Meta: 
        abstract = True

class Foo(SuperFoo):
    ... # do something that changes verbose_name of name field of SuperFoo
Foo._meta.get_field('name').verbose_name = 'Whatever'

这篇关于如何在 Django 中覆盖超类模型字段的详细名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆