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)

我认为是问题所在的示例代码(注释掉公司中的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

我建议,要更新Prepared项目的数据库,首先搜索其当前字段的名称之一,并查看该字段(什么文件)存在于哪里,如modelsforms甚至__init__文件和。。。然后在那里定义你的新领域!您可以在linux中使用grep进行搜索:

grep -E -r "your_field_name or pattern"

-E用于正则表达式模式!

最新更新