Django 1.7 makemigrations - ValueError: cannot serialize class

I am having an interesting problem upgrading from Django 1.6.11 to 1.7. It seems to be based on how I am currently splitting files. Currently, model methods are stored in separate files from models due to the sheer number of methods.

For example, it breaks down like this:

help
|_ modelmethods
|  |_ __init__.py
|  |_ thread_methods.py
|_ __init__.py
|_ models.py

      

__init__.py

in the application folder of the help looks like this:

""" __init__.py for help app."""

from help.modelmethods.thread_methods import *

      

And thread_methods.py looks like this:

"""Methods for the Thread model."""

from help.models import Thread

class ThreadMethods:

    """Adds methods on to the Thread model."""

    def do_the_thing(self):
        pass

Thread.__bases__ += (ThreadMethods,)

      

The error I can see from this is as follows:

Migrations for 'help':
  0001_initial.py:
    - Create model Thread
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 124, in handle
    self.write_migration_files(changes)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 152, in write_migration_files
    migration_string = writer.as_string()
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 129, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 86, in serialize
    arg_string, arg_imports = MigrationWriter.serialize(arg_value)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 245, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 380, in serialize
    raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value)
ValueError: Cannot serialize: <class help.modelmethods.thread_methods.ThreadMethods at 0x1105c3870>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing

      

I understand that it is trying to serialize the class and choke on it. Is there a good way to fix this and keep the separation? Or there will be the only comparable way to split the file models.py

into a model folder with the proper setting __init__.py

, and each file will be allocated to one model, which also contains all the relevant methods (and also to ensure no circular imports were introduced).

+3


source to share


3 answers


You need to get your model methods from the object class , also try taking Thread from ThreadMethods instead of adding it to the __ base __ .



class ThreadMethods(object):
    # ....

      

+1


source


This can happen for many reasons, in my case I installed default=User.pk

for user

which was causing the problem. My django version1.9



class Blog(models.Model): title = models.CharField(max_length=200) content = HTMLField() pub_date = models.DateTimeField('date published', auto_now_add=True) last_updated = models.DateTimeField('date published',default=timezone.now) user = models.ForeignKey(User, default=User.pk)#wrong user = models.ForeignKey(User, default=1)#correct, use any default value featured = models.ImageField(upload_to = 'featured', blank=True)

+1


source


I was unable to complete the migration due to a custom validator. My problem was that I didn't read the manual , which says:

If a class based validator is used in the model field of validators, you must ensure that it is serializable by migrating by adding deconstruct()

and methods __eq__()

.

Which points to the migrations-docs that explain why you need it deconstruct()

and __eq__()

and how to write it.

Should also work for other classes, not just validators.

+1


source







All Articles