Django Model使用ListFields迁移数据库(djangotoolbox)

时间:2014-06-16 15:30:11

标签: django python-2.7 django-models django-south listfield

我已经搜索过,但没有找到问题的解决方案...感谢您的帮助! 我想将我的模型迁移到我的数据库,但我在迁移时有错误。

在我的Models和mysql数据库中使用djangotoolbox中的ListField之前,我没有任何错误。我想我必须将它们定义为djangotoolbox.fields.ListField,但它似乎并没有认出它。我究竟做错了什么? 提前谢谢!

我拥有的数据是2-uplets的列表。例如 : listeAGE = [[12.3,0.8],[16.5,0.6],[85.4,2.6],...]

这是我的django模型:

from djangotoolbox.fields import ListField
class Abaque(models.Model):
      listeAGE = ListField(models.FloatField(default=0))

以下是迁移的基础:

from south.db import db
from south.v2 import SchemaMigration
from django.db import models
from djangotoolbox import fields
class Migration(SchemaMigration):

    # Adding model 'Abaque'
    db.create_table(u'ageBio_abaque', (
        (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
        ('listeAGE', self.gf('django.db.models.fields.FloatField')(default=False)),
    ))
    db.send_create_signal(u'ageBio', ['Abaque'])

    def backwards(self, orm):

    models = {
    u'ageBio.abaque': {
        'Meta': {'object_name': 'Abaque'},
        u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
        'listeAGE': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeIMC': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeRTH': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeTAS': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeCHOL': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeGLY': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeTRIG': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeHEMO': ('djangotoolbox.fields.ListField',{'default': '0'}),
        'listeVGM': ('djangotoolbox.fields.ListField',{'default': '0'}),
     },
     }

然后我创建我的数据库,syncdb,migrate但是我有以下错误:

 Traceback (most recent call last):
 File "C:\Users\Blablabla\workspace\ageBioDjango\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
 File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
 File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
 File "C:\Python27\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
 File "C:\Python27\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
 File "C:\Python27\lib\site-packages\south\management\commands\migrate.py", line 111, in handle
ignore_ghosts = ignore_ghosts,
 File "C:\Python27\lib\site-packages\south\migration\__init__.py", line 220, in migrate_app
success = migrator.migrate_many(target, workplan, database)
 File "C:\Python27\lib\site-packages\south\migration\migrators.py", line 254, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
 File "C:\Python27\lib\site-packages\south\migration\migrators.py", line 329, in migrate_many
result = self.migrate(migration, database)
 File "C:\Python27\lib\site-packages\south\migration\migrators.py", line 133, in migrate
result = self.run(migration, database)
 File "C:\Python27\lib\site-packages\south\migration\migrators.py", line 106, in run
south.db.db.current_orm = self.orm(migration)
 File "C:\Python27\lib\site-packages\south\migration\migrators.py", line 279, in orm
return migration.orm()
  File "C:\Python27\lib\site-packages\south\utils\__init__.py", line 62, in method
value = function(self)
  File "C:\Python27\lib\site-packages\south\migration\base.py", line 432, in orm
return FakeORM(self.migration_class(), self.app_label())
  File "C:\Python27\lib\site-packages\south\orm.py", line 48, in FakeORM
_orm_cache[args] = _FakeORM(*args)  
  File "C:\Python27\lib\site-packages\south\orm.py", line 127, in __init__
self.models[name] = self.make_model(app_label, model_name, data)
  File "C:\Python27\lib\site-packages\south\orm.py", line 320, in make_model
   raise ValueError("Field '%s' on model '%s.%s' has a weird definition length (should be 1 or 3 items)." % (fname, app, name))
ValueError: Field 'listeAGE' on model 'ageBio.Abaque' has a weird definition length (should be 1 or 3 items).
Finished "C:\Users\Blabla\workspace\ageBioDjango\manage.py migrate" execution.

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

   u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
   'listeAGE': ('djangotoolbox.fields.ListField',{'default': '0'}),

请注意id字段定义与列表字段之间的差异,我猜您是手动添加的...您的元组只有两个元素而id有三个

http://south.readthedocs.org/en/latest/customfields.html#south-field-triple

South似乎期待任何一个(只是班级)或全部三个(班级+ args列表+ kwargs dict)

所以你需要这样做:

   'listeAGE': ('djangotoolbox.fields.ListField', [], {'default': '0'}),
   etc