从基础 Django 模型更改 Class 属性 属性(verbose_name 示例)
change Class property properties (verbose_name exmpl.) from base Django model
我有一个摘要 class 带有 name 和 slug field.when 我继承自另一个 class 我想根据 class 更改 verbose_name 参数。我该如何设置这个属性?我想将名称字段的 verbose_name 参数设置为“foo”。但我想对两个不同的 classes 使用不同的参数数据。
一个例子:
对于 ProductModel 名称字段 verbose_name="Foo"
对于 CategoryModel 名称字段 verbose_name="Poo"
class BaseProductModel(models.Model):
name = models.CharField(max_length=200)
slug = AutoSlugField(populate_from="name",unique=True,verbose_name="Slug")
class Meta:
abstract=True
class ProductModel(BaseProductModel):
# I want to set the verbose_Name parameter of the name field to "foo".
#The two fields (name and slug) come from the inherited (BaseProductModel) class.
description = models.TextField(max_length=500,verbose_name="Detay")
class CategoryModel(BaseProductModel):
# The two fields (name and slug) come from the inherited (BaseProductModel) class.
# I want to set the verbose_Name parameter of the name field to "Poo".
def __str__(self):
return self.name
您可以通过两种方式做到这一点
首先,仅适用于抽象 classes,否则它也会更改父 class 的 verbose_name:
class ProductModel(BaseProductModel):
# I want to set the verbose_Name parameter of the name field to "foo".
#The two fields (name and slug) come from the inherited (BaseProductModel) class.
description = models.TextField(max_length=500,verbose_name="Detay")
ProductModel.get_field('name').verbose_name = 'Foo'
其次,在 __init__
方法中执行此操作:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
field = self._meta.get_field('name')
field.verbose_name = 'Foo'
我有一个摘要 class 带有 name 和 slug field.when 我继承自另一个 class 我想根据 class 更改 verbose_name 参数。我该如何设置这个属性?我想将名称字段的 verbose_name 参数设置为“foo”。但我想对两个不同的 classes 使用不同的参数数据。
一个例子:
对于 ProductModel 名称字段 verbose_name="Foo"
对于 CategoryModel 名称字段 verbose_name="Poo"
class BaseProductModel(models.Model):
name = models.CharField(max_length=200)
slug = AutoSlugField(populate_from="name",unique=True,verbose_name="Slug")
class Meta:
abstract=True
class ProductModel(BaseProductModel):
# I want to set the verbose_Name parameter of the name field to "foo".
#The two fields (name and slug) come from the inherited (BaseProductModel) class.
description = models.TextField(max_length=500,verbose_name="Detay")
class CategoryModel(BaseProductModel):
# The two fields (name and slug) come from the inherited (BaseProductModel) class.
# I want to set the verbose_Name parameter of the name field to "Poo".
def __str__(self):
return self.name
您可以通过两种方式做到这一点
首先,仅适用于抽象 classes,否则它也会更改父 class 的 verbose_name:
class ProductModel(BaseProductModel):
# I want to set the verbose_Name parameter of the name field to "foo".
#The two fields (name and slug) come from the inherited (BaseProductModel) class.
description = models.TextField(max_length=500,verbose_name="Detay")
ProductModel.get_field('name').verbose_name = 'Foo'
其次,在 __init__
方法中执行此操作:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
field = self._meta.get_field('name')
field.verbose_name = 'Foo'