【发布时间】:2015-01-21 21:28:51
【问题描述】:
我正在将我的一个 Django 项目从 1.2 升级到 1.6.6。我在尝试调用模型成员时遇到了Cannot resolve keyword 'username' into field 错误。似乎无法识别用“wrap_model”包装的所有字段。这是wrap_model()的代码
def wrap_model(o):
from django.conf import settings
FIELD_LABELS=getattr(settings,'MODEL_FIELDS',{})
object_name=o._meta.object_name
if FIELD_LABELS:
for f in o._meta.local_fields:
fl=None
full_field_name='%s__%s' % (object_name,f.name)
if full_field_name in FIELD_LABELS:
fl=FIELD_LABELS[full_field_name]
elif f.name in FIELD_LABELS:
fl=FIELD_LABELS[f.name]
if fl:
if isinstance(fl,dict):
for k,v in list(fl.items()):
setattr(f,k,v)
else:
f.verbose_name=fl
return o
这是我的模型的代码
@wrap_model
class Member(models.Model):
username = wrap(
models.EmailField(
_('Email:'), unique=True), size='50'
)
password = PasswordField(
_('Password:'),
max_length=50,
help_text='(type and retype password to change)'
)
title = wrap(
models.CharField(
_('Title'),
max_length=50,
null=True,
default='',
blank=True
),
size='50'
)
fname = wrap(
models.CharField(
_('First Name:'),
max_length=50,
null=False,
default=''
),
size='25',
grp=1
)
lname = wrap(
models.CharField(
_('Last Name:'),
max_length=50,
null=False,
default=''
),
size='25',
grp=2
)
created = models.DateTimeField(
auto_now_add=True
)
modified = models.DateTimeField(
editable=False,
blank=True,
null=True,
auto_now=True
)
accessed = models.DateTimeField(blank=True, null=True)
显然那些被wrap_model() 函数包裹的字段无法识别,而没有包裹的字段(例如创建、修改、访问)是正常的。
非常感谢任何帮助!
【问题讨论】:
-
wrap函数是什么? -
由于您只是迁移到 1.6,我建议您查看 South 以创建此迁移。最好在旧模型代码和新模型代码之间进行清晰的分离,并将迁移到南方。 south.aeracode.org
标签: python django django-models python-3.4