【问题标题】:Django Migration Fails Custom FieldDjango 迁移失败自定义字段
【发布时间】:2014-10-24 15:23:04
【问题描述】:

我正在尝试更新 Python 2.4/Django 1.2 上的项目。

该项目在更新之前一直有效。运行 python manage.py 时迁移

我明白了:

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

【问题讨论】:

    标签: python django python-2.7 django-models


    【解决方案1】:

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

    grep -E -r "your_field_name or pattern"

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

    【讨论】:

      猜你喜欢
      • 2011-10-28
      • 2020-05-29
      • 2017-06-17
      • 2013-07-05
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多