我已将项目从Django1.3升级到Django1.5 但是我收到错误“AttributeError:class Meta没有属性'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'”
kwargs[TestIP.Meta.verbose_name_plural.lower().replace(' ', '')] = table
在Django1.3中工作正常,请帮助解决这个问题。
提前致谢
答案 0 :(得分:1)
我想出了答案
在Django1.5中,我们应该像'TestIP._meta.verbose_name_plural'那样访问元类
我已经改变了
kwargs[TestIP.Meta.verbose_name_plural.lower().replace(' ', '')] = table
到
kwargs[TestIP._meta.verbose_name_plural.lower().replace(' ', '')] = table
在django1.5
中工作正常