How to upgrade Django app from 1.6.11 to 1.8 when makemigrations crashes?
Question: I'm following the instructions to upgrade a Django app from Django 1.6.11 to
1.8.1. I've deleted all the migrations save for the **init**.py. I then run:

    
    
    manage.py makemigrations
    

and it crashes with the following traceback:

    
    
    Traceback (most recent call last):
    File "manage.py", line 12, in <module>
      execute_from_command_line(sys.argv)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
      utility.execute()
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
      output = self.handle(*args, **options)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
      self.write_migration_files(changes)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
      migration_string = writer.as_string()
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
      operation_string, operation_imports = OperationWriter(operation).serialize()
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
      _write(arg_name, arg_value)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 75, in _write
      arg_string, arg_imports = MigrationWriter.serialize(item)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
      item_string, item_imports = cls.serialize(item)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
      return cls.serialize_deconstructed(path, args, kwargs)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
      arg_string, arg_imports = cls.serialize(arg)
    File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 465, in serialize
      "topics/migrations/#migration-serializing" % (value, get_docs_version())
    ValueError: Cannot serialize: <bound method AccountManager.gen_num of <coreapi.models.AccountManager object at 0x103925450>>
    There are some values Django cannot serialize into migration files.
    For more, see https://docs.djangoproject.com/en/1.8/topics/migrations/#migration-serializing
    

This function is a method defined in the AccountManager class, which derives
from models.Manager:

    
    
    def gen_num(self):
        max_digits = 9
        candidate = random.randint(0, 10**max_digits)
        return candidate
    

I've inspected the return candidate line and as expected, it's an int.

I assume that this method is being introspected because it is set as the
default for the associated field:

    
    
    num = models.PositiveIntegerField(
        _('Num'), help_text=_('Number, automatically-generated'),
        unique=True, blank=True, null=True, default=AccountManager().gen_num
    )
    


Answer: The solution is to follow the advice given here:

<https://docs.djangoproject.com/en/1.8/topics/migrations/#migration-
serializing>

That is, move the function from the class body of the Manager to the module
body:

    
    
    def gen_num():
        max_digits = 9
        candidate = random.randint(0, 10**max_digits)
        return candidate
    

After this change, makemigrations completes successfully.

