另一种方法是重写ContentType.__str__
方法,因为它看起来是这样的:
def __str__(self):
# self.name is deprecated in favor of using model's verbose_name, which
# can be translated. Formal deprecation is delayed until we have DB
# migration to be able to remove the field from the database along with
# the attribute.
#
# We return self.name only when users have changed its value from the
# initial verbose_name_raw and might rely on it.
model = self.model_class()
if not model or self.name != model._meta.verbose_name_raw:
return self.name
else:
return force_unicode(model._meta.verbose_name)
所以,你可以重写它,如果你不需要某种向后兼容的:
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode
def contenttype_as_str(self):
return force_unicode(self.model_class()._meta.verbose_name)
ContentType.__str__ = contenttype_as_str
这有点棘手,但我相信它更直接。请注意,因为使用了Django 1.4.1 force_text
而不是force_unicode
。
对我很好,thx!顺便说一句,你应该把这个标记为答案:) – Dave
如果你在你的详细名称中使用特殊字符,你想使用'new_name = cl._meta.verbose_name.decode('utf-8')' –
如果你想知道像我**放哪里这个代码**,应用程序级'__init __。py'是地方。 –