Django迁移失败自定义字段

时间:2014-10-24 15:23:05

标签: python django python-2.7 django-models

我正在尝试将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

1 个答案:

答案 0 :(得分:0)

我建议,为了更新预备项目的数据库,首先搜索其当前字段的名称之一,并查看该字段(哪些文件)的存在位置,如modelsforms甚至{ {1}}文件和......然后在那里定义你的新字段!您可以在linux中使用__init__进行搜索:

grep

grep -E -r "your_field_name or pattern"用于使用正则表达式模式!