Python django cms:插件模型与其数据库表不同步
在将django cms从2.4.3版升级到3.0.11版(目前是3.0.12版)之后,我意识到一些模型与其数据库表“不同步”。例如:Python django cms:插件模型与其数据库表不同步,python,django,django-south,django-cms,Python,Django,Django South,Django Cms,在将django cms从2.4.3版升级到3.0.11版(目前是3.0.12版)之后,我意识到一些模型与其数据库表“不同步”。例如: class ProjectPagePluginModel(cmsPlugin): """ CMS project plugin model. """ project = models.ForeignKey(Project, on_delete=CASCADE) max_occurrences = models.Positi
class ProjectPagePluginModel(cmsPlugin):
"""
CMS project plugin model.
"""
project = models.ForeignKey(Project, on_delete=CASCADE)
max_occurrences = models.PositiveIntegerField(default=0)
def __unicode__(self):
return self.get_plugin_name()
>>> ProjectPagePluginModel.objects.all()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 71, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
self._fetch_all()
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
ProgrammingError: relation "project_projectpagepluginmodel" does not exist
LINE 1: ...ct_projectpagepluginmodel"."max_occurrences" FROM "project_p...
更新:manage.py也不迁移
更新2:
我迁移的某个步骤出错了,我找到了丢失的信息
首先,我进入postgresql shell以了解此插件信息的位置:
unicms2=> select * from information_schema.tables where table_name like '%projectpage%';
table_catalog | table_schema | table_name | table_type | self_referencing_column_name | reference_generation | user_defined_type_catalog | user_defined_type_schema | user_defined_type_name | is_insertable_into | is_typed | commit_action
---------------+--------------+----------------------------------+------------+------------------------------+----------------------+---------------------------+--------------------------+------------------------+--------------------+----------+---------------
unicms2 | public | cmsplugin_projectpagepluginmodel | BASE TABLE | | | | | | YES | NO |
(1 row)
unicms2=> select count(*) from cmsplugin_projectpagepluginmodel;
count
-------
39180
(1 row)
然后,我进入django shell,使用[south][1]重命名该表
>>> from south.db import db
>>> db.rename_table('cmsplugin_projectpagepluginmodel', 'project_projectpagepluginmodel')
我原以为这会奏效,但后来我开始出现运行时错误:
ProgrammingError: relation "cmsplugin_projectpagepluginmodel" does not exist
LINE 1: ...in_projectpagepluginmodel"."max_occurrences" FROM "cmsplugin...
我找到了解决这个错误的方法。不知何故,正如我所说,数据库与ORM不同步——我认真地认为这与django cms 3的虚假升级有关
我必须按如下方式审查我的所有申请:
$ python manage.py dumpdata >> fixture.json
CommandError: Unable to serialize database: relation "staff_staffpluginmodel" does not exist
LINE 1: ..., "staff_staffpluginmodel"."max_occurrences" FROM "staff_sta..."
$ mv staff/migrations/ ~/migrations-BAK/staff_migrations
$ python manage.py shell
>> from south.models import MigrationHistory
>> MigrationHistory.objects.filter(app_name='staff').delete()
>> quit()
$ python manage.py schemamigration --initial staff
$ vim staff/migrations/0001_initial.py # comment out every command within forwards() except of those which create "staff_staffpluginmodel"
$ python manage.py migrate staff
$ python manage.py dumpdata >> fixture.json
$ python manage.py datamigration staff migrate --freeze staff
-> edit the created staff/migrations/XXX.py migration, with the customised forwards and backwards methods
$ python manage.py migrate staff
-> enjoy
如果最后一个命令出现任何其他错误,只需重复该过程,直到确定为止
当然,这可以通过djangoshell使用纯南方迁移来完成,而不是删除迁移文件和迁移历史。但是--auto参数使生活更轻松;-)
编辑->使用南方数据迁移的更干净的解决方案如下:
$ python manage.py dumpdata >> fixture.json
CommandError: Unable to serialize database: relation "staff_staffpluginmodel" does not exist
LINE 1: ..., "staff_staffpluginmodel"."max_occurrences" FROM "staff_sta..."
$ mv staff/migrations/ ~/migrations-BAK/staff_migrations
$ python manage.py shell
>> from south.models import MigrationHistory
>> MigrationHistory.objects.filter(app_name='staff').delete()
>> quit()
$ python manage.py schemamigration --initial staff
$ vim staff/migrations/0001_initial.py # comment out every command within forwards() except of those which create "staff_staffpluginmodel"
$ python manage.py migrate staff
$ python manage.py dumpdata >> fixture.json
$ python manage.py datamigration staff migrate --freeze staff
-> edit the created staff/migrations/XXX.py migration, with the customised forwards and backwards methods
$ python manage.py migrate staff
-> enjoy
重复上述代码段,直到不再出现编程错误