我尝试添加我的模型降价,但出现错误。我的旧models.py:
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
excerpt = models.TextField(blank=True, help_text="A small teaser of\
your content")
content = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=True)
objects = models.Manager()
published_objects = PublishedManager()
tags = TaggableManager()
category = models.ForeignKey(Category)
Run Code Online (Sandbox Code Playgroud)
和我的新models.py
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
excerpt = models.TextField(blank=True, help_text="A small teaser of\
your content")
content = models.TextField()
contentmarkdown = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=True)
objects = models.Manager()
published_objects = PublishedManager()
tags = TaggableManager() …Run Code Online (Sandbox Code Playgroud) 我将这些字段添加到我的模型中:
class WatchList(models.Model):
name = models.CharField(max_length=20)
class Thing(models.Model):
watchlist = models.ForeignKey(WatchList)
Run Code Online (Sandbox Code Playgroud)
成功运行架构迁移:
>>> $ python2.7 manage.py schemamigration myapp --auto
+ Added model myapp.WatchList
? The field 'Thing.watchlist' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的应用程序从 Django v1.6.11 升级到 v1.7.8。我按照说明从南方升级,但一次又一次遇到相同的错误。更确切地说:
\n\n$ python manage.py migrate makemigrations\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/reversion/admin.py:385: RemovedInDjango18Warning: commit_on_success is deprecated in favor of atomic.\n def recover_view(self, request, version_id, extra_context=None):\n\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/reversion/admin.py:397: RemovedInDjango18Warning: commit_on_success is deprecated in favor of atomic.\n def revision_view(self, request, object_id, version_id, extra_context=None):\n\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/django/forms/widgets.py:143: RemovedInDjango18Warning: `VersionMetaAdmin.queryset` method should be renamed `get_queryset`.\n .__new__(mcs, name, bases, attrs))\n\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/cms/publisher/manager.py:5: RemovedInDjango18Warning: `PublisherManager.get_query_set` method should be renamed `get_queryset`.\n class PublisherManager(models.Manager):\n\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/cms/models/managers.py:15: RemovedInDjango18Warning: `PageManager.get_query_set` method should be renamed `get_queryset`.\n class PageManager(PublisherManager):\n\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/cms/admin/change_list.py:39: RemovedInDjango18Warning: `CMSChangeList.get_query_set` method should be renamed `get_queryset`.\n class CMSChangeList(ChangeList):\n\n/home/roberto/virtualenvs/ve_unicms-django1.7/local/lib/python2.7/site-packages/cms/admin/forms.py:397: RemovedInDjango18Warning: Creating …Run Code Online (Sandbox Code Playgroud) 我定义了一个抽象类,它有一个ForeignKey. 我有多个派生模型类,但是当我尝试生成架构迁移脚本时,south 向我显示错误。
class BlogEntryBase(models.Model):
author = models.CharField(null=True, blank=True, max_length=100)
title = models.CharField(null=True, blank=True, max_length=255)
created_by = models.ForeignKey("main.UserProfile", verbose_name="Created By", related_name="%(class)s_set", blank=False, null=False)
class CatBlogEntry(BlogEntryBase):
pass
class DogBlogEntry(BlogEntryBase):
pass
Run Code Online (Sandbox Code Playgroud)
错误信息:
animal.catblogentry: Accessor for field 'created_by' clashes with related field 'UserProfile.catblogentry_set'. Add a related_name argument to the definition for 'created_by'.
animal.catblogentry: Reverse query name for field 'created_by' clashes with related field 'UserProfile.catblogentry_set'. Add a related_name argument to the definition for 'created_by'.
animal.dogblogentry: Accessor for field 'created_by' clashes with related …Run Code Online (Sandbox Code Playgroud) python django abstract-class foreign-key-relationship django-south
我正在使用South进行Django项目的数据库迁移.我想知道将我的开发服务器生成的迁移脚本提交到存储库然后在生产服务器上重用它是否是一个好主意?
我在使用schemamigration将列添加到数据库时遇到了问题.有问题的字段'is_flagged'是属于我的应用'upload'中的视频模型的布尔值.运行迁移时,我得到以下内容:
....:~/..../webapp$ python manage.py schemamigration upload --auto
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 219, in execute
self.validate()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 36, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 61, …Run Code Online (Sandbox Code Playgroud) 我有一个Django项目,我在其中添加了南方应用程序,使数据库迁移更容易.
我在项目的几个有限位置使用Django单元测试框架,这意味着我不会定期运行测试.我最近为代码的新部分编写了一些新的测试,并尝试运行它们.我收到以下错误:
django.db.utils.DatabaseError: no such table: auth_permission
Run Code Online (Sandbox Code Playgroud)
追溯提到了一些南方文件(例如/Library/Python/2.7/site-packages/south/management/commands/test.py).南方可能会对测试跑步者造成伤害吗?
我有和现有的数据库,我已经用SQLAlchemy迁移到一个新的PostgreSQL数据库.
我用与以前相同的值移动了所有主键.现在我有填充数据的表,但相关的序列从1开始.我有pk值存储1到2000.
现在,当我尝试用Django保存一些东西时,我有了
重复键值违反了与主键有关的唯一约束.
如何修改序列起始值或逃避这种情况?
我目前的解决方案是:
conn = psycopg2.connect(...)
for table_name in table_names:
cursor = conn.cursor()
cursor.execute("""
SELECT setval('%s_id_seq', (SELECT COALESCE(MAX(id),0)+1 FROM %s));
"""% (table_name, table_name))
Run Code Online (Sandbox Code Playgroud)
它对我有用,但我不喜欢它.
我和Django南玩耍,和它的力量留下了深刻印象,但在做一些迁移的过程中,我已经成功地做的事情,导致迁移的中间错误.比如在数据迁移文件中出现语法错误或运行时异常,决定我不想实际执行某些操作并在迁移期间点击ctrl-c并过早中止等等.
我正在使用MySQL作为数据库后端.当南方出现问题时,我是否需要担心数据库的完整性?事务是否确保所有问题在出错时回滚?
每当我创建一个datamigration使用Django South(但尚未运行它),然后随后创建一个schemamigration向该迁移中涉及的模型添加新字段时,manage.py migrate任务就失败了datamigration.
原因是models.py文件的类定义包含一个datamigration无法识别的新字段定义.
有什么方法可以防止这种情况吗?
django-south ×10
django ×9
python ×5
database ×1
migration ×1
mysql ×1
postgresql ×1
reusability ×1
unit-testing ×1