2015-07-03 41 views
3

我遇到了一個有趣的問題,從Django 1.6.11升級到1.7。這似乎是基於我目前如何分割文件。目前,由於大量的方法,模型方法存儲在與模型分開的文件中。Django 1.7 makemigrations - ValueError:無法序列化

例如,它被分解如下:

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

__init__.py在幫助app文件夾看起來像這樣:

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

from help.modelmethods.thread_methods import * 

而且thread_methods.py看起來是這樣的:

"""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,) 

我從中看到的錯誤如下:

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 

我意識到它試圖序列化這個類並對它進行窒息。有沒有解決這個問題並保持分離的好方法?或者,唯一可比較的方法是將models.py文件拆分爲具有正確__init__.py設置的模型文件夾,並且每個文件專用於同時包含所有相關方法(以及確保沒有引入循環導入)的一個模型。

回答

1

你需要從對象類派生你的方法的模型,也儘量從ThreadMethods派生線程,而不是將它添加到__bases__來得到的。

class ThreadMethods(object): 
    # .... 
+0

有趣。我會試一試並回復你。 – bagelbits

+0

完全有效。我想我可能會遇到的唯一問題是用戶模型,但這可能適用於它:http://stackoverflow.com/questions/1818223/best-way-to-add-convenience-methods-to-a-django-auth -user模型 – bagelbits

1

會發生這種情況的原因有很多,在我的情況,這是我設置爲default=User.pk哪個user是造成問題。我的Django的版本是1.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

我無法因爲一個自定義驗證的遷移。我的問題是,我沒有讀過the manual正確,在那裏說:

If a class-based validator is used in the validators model field option, you should make sure it is serializable by the migration framework by adding deconstruct() and __eq__() methods.

指向這解釋了爲什麼你需要的deconstruct()__eq__()以及如何將它們寫migrations-docs

還應該適用於其他類而不僅僅用於驗證器。

相關問題