Django 1.7 makemigrations - ValueError:无法序列化函数:lambda

时间:2014-10-08 12:02:50

标签: python django

我切换到Django 1.7。当我为我的应用程序尝试makemigrations时,它会崩溃。崩溃报告是:

Migrations for 'roadmaps':
  0001_initial.py:
    - Create model DataQualityIssue
    - Create model MonthlyChange
    - Create model Product
    - Create model ProductGroup
    - Create model RecomendedStack
    - Create model RecomendedStackMembership
    - Create model RoadmapMarket
    - Create model RoadmapUser
    - Create model RoadmapVendor
    - Create model SpecialEvent
    - Create model TimelineEvent
    - Create model UserStack
    - Create model UserStackMembership
    - Add field products to userstack
    - Add field viewers to userstack
    - Add field products to recomendedstack
    - Add field product_group to product
    - Add field vendor to product
    - Add field product to dataqualityissue
Traceback (most recent call last):
  File "manage.py", line 29, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-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/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 124, in handle
    self.write_migration_files(changes)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 152, in write_migration_files
    migration_string = writer.as_string()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 129, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 80, in serialize
    arg_string, arg_imports = MigrationWriter.serialize(item)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 245, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 310, in serialize
    return cls.serialize_deconstructed(path, args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 221, in serialize_deconstructed
    arg_string, arg_imports = cls.serialize(arg)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 323, in serialize
    raise ValueError("Cannot serialize function: lambda")
ValueError: Cannot serialize function: lambda

我在https://code.djangoproject.com/ticket/22892

找到了关于此的说明

还有文档https://docs.djangoproject.com/en/dev/topics/migrations/#serializing-values

的链接

但它并没有让我更清楚。错误消息没有给我一个线索在哪里寻找问题。

有没有办法检测究竟是什么线导致问题?

任何提示?

2 个答案:

答案 0 :(得分:9)

我们在自定义字段定义中使用lambda时遇到了这个问题。

由于没有在回溯中列出,因此很难发现,并且在使用此类自定义字段的特定模型上不会引发错误。

我们的解决方法:

  • 检查所有自定义字段(即使在第三方库中)
  • 将lambda更改为callable,它在模块中定义(即不在自定义字段类中)

答案 1 :(得分:0)

我花了一些时间来解决这个问题,但是@Radek建议的代码示例。

lambda替换为函数的示例。

破解版本:

class SomeModel(ParentModel):
    thing_to_export = ArrayField(models.CharField(max_length=50), 
                                 default=lambda: ['Default thing'])

工作版本:

def default_thing():
    return ['THIS IS A DEFAULT']

class SomeModel(ParentModel):
    thing_to_export = ArrayField(models.CharField(max_length=50), 
                                 default=default_thing)