Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.7 makemigrations - ValueError: Cannot serialize function: lambda

Tags:

python

django

I switch to Django 1.7. When I try makemigrations for my application, it crash. The crash report is:

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

I found a note about that here https://code.djangoproject.com/ticket/22892

There is also link to documentation https://docs.djangoproject.com/en/dev/topics/migrations/#serializing-values

But it does not make it clearer for me. The error meassage have not gave me a clue where to look for problem.

Is there way how to detect what line exactly cause the problem?

Any hints?

like image 380
matousc Avatar asked Oct 08 '14 12:10

matousc


2 Answers

We had this issue with using lambda in the custom field definition.

It is than hard to spot as it is not listed in traceback and the error is not raised on the particular model which uses such custom field.

Our way to fix:

  • check all your custom fields (even in the 3rd party libraries)
  • change the lambda to callable, which is defined in the module (i.e. not in custom field class)
like image 148
Radek Avatar answered Oct 17 '22 11:10

Radek


It took me a bit of time to figure this out, but a code example of what @Radek suggested.

An example replacing the lambda with a function.

Borken version:

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

Working version:

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

class SomeModel(ParentModel):
    thing_to_export = ArrayField(models.CharField(max_length=50), 
                                 default=default_thing)
like image 1
jmunsch Avatar answered Oct 17 '22 10:10

jmunsch