Django迁移失败自定义字段
我正在尝试把一个使用Python 2.4和Django 1.2的项目更新到最新版本。
这个项目之前运行得很好,但在更新后出现了问题。当我运行命令python manage.py migrate时,
我遇到了以下错误:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File manage.py, line 21, in <module>
execute_from_command_line(sys.argv)
File /usr/local/lib/python2.7/site-packages/django/core/management/__init__.py, line 385, in execute_from_command_line
utility.execute()
File /usr/local/lib/python2.7/site-packages/django/core/management/__init__.py, line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File /usr/local/lib/python2.7/site-packages/django/core/management/base.py, line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File /usr/local/lib/python2.7/site-packages/django/core/management/base.py, line 338, in execute
output = self.handle(*args, **options)
File /usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py, line 160, in handle
executor.migrate(targets, plan, fake=options.get(fake, False))
File /usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py, line 63, in migrate
self.apply_migration(migration, fake=fake)
File /usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py, line 91, in apply_migration
if self.detect_soft_applied(migration):
File /usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py, line 135, in detect_soft_applied
apps = project_state.render()
File /usr/local/lib/python2.7/site-packages/django/db/migrations/state.py, line 54, in render
real_models.append(ModelState.from_model(model, exclude_rels=True))
File /usr/local/lib/python2.7/site-packages/django/db/migrations/state.py, line 182, in from_model
e,
TypeError: Couldn't reconstruct field map_type on core.AffiliateMap: __init__() takes at least 2 arguments (1 given)
我认为问题出在这段示例代码上(如果把Company中的EnumerationField注释掉,错误就消失了):
class Company(models.Model):
CATEGORIES = ['Unspecified'] + settings.SMS_COMPANY_CATEGORIES
category = EnumerationField(enum=CATEGORIES, default=CATEGORIES[0])
class EnumerationField(models.PositiveSmallIntegerField):
__metaclass__ = models.SubfieldBase
def __init__(self, enum, *args, **kwargs):
self.enum = enum
self.enum_dict = dict(zip(enum, range(len(enum))))
kwargs['choices'] = [(v, v) for v in enum]
if 'default' in kwargs:
value = kwargs['default']
if value in enum:
kwargs['default'] = self.enum_dict[value]
else:
raise ValueError(No %s value in enumeration % value)
print args = %snkwargs = %s % (args, kwargs)
sys.stdout.flush()
models.PositiveSmallIntegerField.__init__(self, *args, **kwargs)
def to_python(self, value):
if type(value) is int:
if not value in range(len(self.enum)):
raise IndexError('Index %s out of range in enumeration' % value)
return self.enum[value]
else:
if not value:
return ''
if not value in self.enum:
raise ValueError(No %s value in enumeration % value)
return value
def get_prep_value(self, value):
if value in self.enum:
return self.enum_dict[value]
else:
raise ValueError(No %s value in enumeration % value)
def get_prep_lookup(self, lookupType, value):
if lookupType == 'exact':
return self.get_prep_value(value)
elif lookupType == 'in':
return [self.get_prep_value(v) for v in value]
else:
raise TypeError('Lookup type %s not supported' % lookupType
1 个回答
0
我建议在更新一个准备好的项目的数据库时,先搜索一下当前字段的名字,看看这个字段在哪些文件里存在,比如 models
、forms
,甚至是 __init__
文件等等。然后在这些地方定义你的新字段!你可以在Linux系统中使用 grep
命令来搜索:
grep -E -r "你的字段名或模式"
-E
是用来使用正则表达式模式的!