我创建了一个Django应用程序,它有自己的内部投票系统和一个名为Vote的模型来跟踪它.我想将投票系统重构为自己的应用程序,以便我可以重用它.但是,原始应用程序正在生产中,我需要创建一个数据迁移,它将获取所有投票并将它们移植到单独的应用程序中.
如何让两个应用程序参与迁移,以便我可以访问他们的模型?不幸的是,原始和单独的应用程序现在都有一个名为Vote的模型,所以我需要知道任何冲突.
我有一些新的组,我想添加到Django的"auth_group"表,我更喜欢使用South将这些数据"迁移"到数据库中.不幸的是,我不确定我应该采取什么步骤来创建迁移文件,然后让它加载我的夹具.
有什么想法吗?
我有一个南数据迁移,试图根据其他模型中的数据创建新对象.在尝试为给定的"目标"模型创建新对象时,我不断获得:
Cannot assign "<ContentType: ContentType object>": "Publishing.content_type" must be a "ContentType" instance.
Run Code Online (Sandbox Code Playgroud)
当通过South冻结ORM访问时,"实例"似乎有问题,例如:
ContentType = orm['contenttypes.ContentType']
content_type_kwargs = {
'model': ContentModel._meta.module_name,
'app_label': ContentModel._meta.app_label, }
content_type = ContentType.objects.get(**content_type_kwargs)
# further down
publishing_kwargs = {
'site': Site.objects.get_current(),
'publishing_type': publishing_type,
'start': start,
'content_type': content_type,
'object_id': content_object.id, }
publishing = orm.Publishing(**publishing_kwargs) # Produces the error above
Run Code Online (Sandbox Code Playgroud)
现在我已多次验证它content_type实际上是ContentType的一个实例 - 但不知何故django并不这么认为.
本来,模型属性位置为项目类的定义如下所示:
location = models.ForeignKey('Location', related_name='+', null=True, on_delete=models.SET_NULL)
Run Code Online (Sandbox Code Playgroud)
然后重新定义为:
location = models.ForeignKey('Location', related_name='+', on_delete=models.PROTECT)
Run Code Online (Sandbox Code Playgroud)
由于定义的变化,我执行了South的模式移植.南回应
字段'Item.location'没有指定默认值,但是NOT NULL.由于您要使此字段不可为空,因此您必须指定用于现有行的默认值.
我选择了'2'并提供了现有位置的PK(整数).
但是当我运行迁移时,我收到以下错误:
django.db.utils.IntegrityError:列"location_id"包含空值
我不明白为什么在提供有效的默认位置PK时出现此错误.这真是令人难以置信.请帮忙〜谢谢.
迁移规范:
def forwards(self, orm):
# Changing field 'Item.location'
db.alter_column('lend_borrow_item', 'location_id', self.gf('django.db.models.fields.related.ForeignKey')(default=11, to=orm['app_name.Location']))
def backwards(self, orm):
# Changing field 'Item.location'
db.alter_column('lend_borrow_item', 'location_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['app_name.Location']))
models = {
'app_name.location': {
'Meta': {'ordering': "['name']", 'object_name': 'Location'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '20'})
},
'lend_borrow.item': {
'Meta': {'object_name': 'Item'},
'id': …Run Code Online (Sandbox Code Playgroud) 我正在使用django创建一个博客.它安装在虚拟环境中,并已安装django-tagging.我在做DB迁移以南,一切似乎好的工作与我的迁移,但似乎没有被创建的标签表,所以当我去通过管理来添加一个博客文章中,我得到了著名PostgreSQL的错误:
Exception Type: DatabaseError at /admin/bppsite/blogpost/add/
Exception Value: relation "tagging_tag" does not exist
LINE 1: ...ECT "tagging_tag"."id", "tagging_tag"."name" FROM "tagging_t...
Run Code Online (Sandbox Code Playgroud)
这是我的models.py的相关部分:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^tagging\.fields\.TagField"])
from tagging.models import Tag
from tagging.fields import TagField
class BlogPost(models.Model):
title = models.CharField(max_length = 255)
text = models.TextField()
author = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add = True)
modified = models.DateTimeField(auto_now = True)
status = models.CharField(max_length = 10, choices=POST_STATUS_CHOICES, default='DRAFT')
slug = models.SlugField(max_length = 255, blank=True)
category = models.ManyToManyField(Category)
tags = TagField()
def __unicode__(self):
return …Run Code Online (Sandbox Code Playgroud) 每次尝试运行新迁移时,我的南迁移历史记录表都会导致完整性错误.这是错误:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "south_migrationhistory_pkey"
DETAIL: Key (id)=(40) already exists.
Run Code Online (Sandbox Code Playgroud)
到目前为止,这只发生在当地.我已经删除了数据库并重建了很多次,并且每次所有现有迁移都顺利运行.但是一旦我创建了新的迁移,我就会再次收到此错误.
迁移40碰巧是第三方迁移(djangoratings),所以我不认为这是迁移文件的问题.
任何帮助将不胜感激!
我最近将South添加到现有的Django项目中.我经历了整个过程
python manage.py syncdb
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake
Run Code Online (Sandbox Code Playgroud)
根据此票证的最后评论进行处理(因为我有自定义用户模型).
然后我想我做了一个架构迁移和迁移?我不完全记得,但我结束了两个所谓的迁移文件0001_initial.py和0002_initial.py,这似乎并不完全正确.
今天我尝试在我的一个模型中添加一个字段并进行迁移:
$ python manage.py schemamigration myapp --auto
? The field 'Photo.main_person' 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
? …Run Code Online (Sandbox Code Playgroud) 我是Heroku的新手,所以我尝试按字面意思按照说明操作,因为这个错误我迷失了.因此,当我包含在"Heroku上使用Django入门"中编写的settings.py配置时,除非我从已安装的应用程序中注释掉南方,否则我无法再运行本地服务器.
这是错误:
from south.db import DEFAULT_DB_ALIAS
File "/home/alejandro/Proyectos/olenv/local/lib/python2.7/site-packages/south/db/__init__.py", line 83, in <module>
db = dbs[DEFAULT_DB_ALIAS]
KeyError: 'default'
Run Code Online (Sandbox Code Playgroud)
这是settings.py中的相关设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbolib',
'USER': 'alejandro',
'PASSWORD': 'zzzzz'
}
}
# --- HEROKU --- #
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
Run Code Online (Sandbox Code Playgroud)
也许它应该是这样的,因为南方是一个生产工具,因此它与Heroku冲突.也许不是,因为我是Heroku的新手,任何澄清都会有所帮助.
编辑 当我向南注释并尝试运行syncdb时,我发现此错误:
File "/home/alejandro/Proyectos/olenv/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. …Run Code Online (Sandbox Code Playgroud) 我正在升级到Django 1.7.4并使用新的内置迁移,但在尝试运行时出现以下错误makemigrations:
ValueError: Could not find function upload_callback in app_utils.
Please note that due to Python 2 limitations, you cannot serialize unbound
method functions (e.g. a method declared and used in the same class body).
Please move the function into the main module body to use migrations.For more information,
see https://docs.djangoproject.com/en/1.7/topics/migrations/#serializing-values
Run Code Online (Sandbox Code Playgroud)
我的模型定义:
class Discount(models.Model):
banner = models.ImageField(
help_text=_("Banner image for this discount"),
upload_to=upload_to('discounts/', 'title'),
blank=True,
null=True
)
Run Code Online (Sandbox Code Playgroud)
我的上传回调app_utils.py:
def upload_to(path, attribute=None):
def upload_callback(instance, filename):
compact = …Run Code Online (Sandbox Code Playgroud) 我有一个新字段要添加到我的数据库中.所以我说
python manage.py makemigrations
Run Code Online (Sandbox Code Playgroud)
正确创造kernel/migrations/0003_auto_20150726_1911.py.我检查内容,看起来都很好.
我说
python manage.py migrate
Run Code Online (Sandbox Code Playgroud)
我不太开心 向表kernel/migrations/0002_auto_20150707_1459.py添加字段的文件失败.即使我非常确定应用了迁移.因此,永远不会应用迁移0003.date_of_birthuserprofile
这是生产.:(我不知道该怎么办才能申请0003而不是软管django.建议?
其余部分是支持文档:
?? (master=) [virt]??
? [T] django@beta13:migrations $ cat 0002_auto_20150707_1459.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('kernel', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='userprofile',
name='date_of_birth',
field=models.DateField(null=True, blank=True),
),
]
?? (master=) [virt]??
? [T] django@beta13:migrations $ cat 0003_auto_20150726_1911.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from …Run Code Online (Sandbox Code Playgroud) django-south ×10
django ×9
postgresql ×3
python ×3
database ×2
heroku ×1
migration ×1
python-2.7 ×1