我在迁移到 django 1.7 和 django 迁移时发布了一个新答案,解决方案并不明显,我必须创建自己的迁移类来将外键添加到远程表。
from django.db.migrations import AddField
class AddRemoteField(AddField):
def __init__(self, remote_app, *args, **kwargs):
super(AddRemoteField, self).__init__(*args, **kwargs)
self.remote_app = remote_app
def state_forwards(self, app_label, *args, **kwargs):
super(AddRemoteField, self).state_forwards(self.remote_app, *args, **kwargs)
def database_forwards(self, app_label, *args, **kwargs):
super(AddRemoteField, self).database_forwards(
self.remote_app, *args, **kwargs)
def database_backwards(self, app_label, *args, **kwargs):
super(AddRemoteField, self).database_backwards(
self.remote_app, *args, **kwargs)
然后我制作了一个迁移文件:
from __future__ import unicode_literals
from django.db import models, migrations
from my_app.tools import AddRemoteField
from my_app.models import Client
class Migration(migrations.Migration):
dependencies = [
('anikit', '0002_manual_user_migration'),
]
operations = [
AddRemoteField(
remote_app='auth',
model_name='user',
name='client',
field=models.ForeignKey(Client, verbose_name='client',
null=True, blank=True),
preserve_default=True,
),
]