我已将我的项目从 Django1.3 升级到 Django1.5,但出现错误“AttributeError: class Meta has no attribute 'verbose_name_plural'”
基类
class PortalModelBase(models.base.ModelBase):
def __new__(cls, name, bases, attrs):
attr_meta = attrs.pop('Meta', None)
try:
is_global = attr_meta.is_global
del attr_meta.is_global
except AttributeError:
is_global = False
attrs['Meta'] = attr_meta
model = super(PortalModelBase, cls).__new__(cls, name, bases, attrs)
model._meta.is_global = is_global
return model
class Meta:
abstract = True
模型类
将元类作为 PortalModelBase
class PortalModel(models.Model):
__metaclass__ = PortalModelBase
name = models.CharField(max_length=255, verbose_name='name*', help_text='Enter Name', blank=False)
description = models.CharField(max_length=1024, verbose_name='Description', help_text="Enter a description", blank=True,null=True, default='')
createdby = models.CharField(max_length=128)
createdon = models.DateTimeField()
modifiedby = models.CharField(max_length=128)
modifiedon = models.DateTimeField()
def __init__(self_, *args, **kwargs):
log.info("Portal model initializing")
if kwargs:
d = copy.copy(kwargs)
for key in d:
if key not in self_._meta.get_all_field_names():
#The key given by the dictionary is not in the model.
#Django, by design, will throw an exception
#this prevents that
kwargs.pop(key)
return super(PortalModel, self_).__init__(*args, **kwargs)
class Meta:
abstract = True
继承类
class TestIP(PortalModel):
address = models.CharField(max_length=128, verbose_name='VIP')
class Meta:
abstract = False
verbose_name = 'testIP'
verbose_name_plural = "testips"
试图在views.py中调用verbose_name_plural得到错误“AttributeError:类Meta没有属性'verbose_name_plural'”
视图.py
kwargs[TestIP.Meta.verbose_name_plural.lower().replace(' ', '')] = table
它在 Django1.3 中运行良好,请帮助解决这个问题。
提前致谢