看起来这应该是"容易"或至少在某处记录,我只是无法找到它.
让我们说我有一个模型:
class A(models.Model):
users = models.ManyToMany('auth.User', blank=True)
Run Code Online (Sandbox Code Playgroud)
现在我想迁移到一个through表来向ManyToMany关系添加字段...
class AUsers(models.Model):
user = models.ForeignKey('auth.User')
a = models.ForeignKey('A')
new_field = models.BooleanField()
class A(models.Model):
users = models.ManyToMany('auth.User', blank=True, through='AUsers')
Run Code Online (Sandbox Code Playgroud)
然后我做:
% ./manage.py schemamigration app --auto
Run Code Online (Sandbox Code Playgroud)
并不奇怪,它告诉我它将通过表删除原始的自动创建并为其创建一个新的AUsers.目前最好的做法是什么?是否有适当的方式迁移到新through表?我db_table在Meta 使用吗?我是不是through=...马上使用...然后做一个schemamigration --auto,然后a datamigration复制当前表(不知何故,不确定...)然后添加through关系,让它杀死表?
这里的诀窍是什么?这真的很难吗?
我正在尝试向我之前创建的ManyToMany字段添加订单.我基本上想订购图片集中的图片.我在Django 1.7上运行,所以没有更多的南迁移(我试图遵循这个教程:http://mounirmesselmeni.github.io/2013/07/28/migrate-django-manytomany-field-to-manytomany-through -with -南/)
这是我的"通过"关系:
class CollectionPictures(models.Model):
picture = models.ForeignKey(
Picture,
verbose_name=u'Picture',
help_text=u'Picture is included in this collection.',
)
collection = models.ForeignKey(
Collection,
verbose_name=u'Collection',
help_text=u'Picture is included in this collection',
)
order = models.IntegerField(
verbose_name=u'Order',
help_text=u'What order to display this picture within the collection.',
max_length=255
)
class Meta:
verbose_name = u"Collection Picture"
verbose_name_plural = u"Collection Pictures"
ordering = ['order', ]
def __unicode__(self):
return self.picture.name + " is displayed in " + self.collection.name + (
" in …Run Code Online (Sandbox Code Playgroud) migration django manytomanyfield django-1.7 django-migrations
我使用的是 PostGres 10、Python 3.9 和 Django 3.2。我已经建立了这个模型以及伴随的多对多关系......
class Account(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
...
crypto_currencies = models.ManyToManyField(CryptoCurrency)
Run Code Online (Sandbox Code Playgroud)
生成并运行 Django 迁移后,创建了下表...
\d cbapp_account_crypto_currencies;
Table "public.cbapp_account_crypto_currencies"
Column | Type | Modifiers
-------------------+---------+------------------------------------------------------------------------------
id | integer | not null default nextval('cbapp_account_crypto_currencies_id_seq'::regclass)
account_id | uuid | not null
cryptocurrency_id | uuid | not null
Indexes:
"cbapp_account_crypto_currencies_pkey" PRIMARY KEY, btree (id)
"cbapp_account_crypto_cur_account_id_cryptocurrenc_38c41c43_uniq" UNIQUE CONSTRAINT, btree (account_id, cryptocurrency_id)
"cbapp_account_crypto_currencies_account_id_611c9b45" btree (account_id)
"cbapp_account_crypto_currencies_cryptocurrency_id_685fb811" btree (cryptocurrency_id)
Foreign-key constraints:
"cbapp_account_crypto_account_id_611c9b45_fk_cbapp_acc" FOREIGN KEY (account_id) REFERENCES cbapp_account(id) DEFERRABLE INITIALLY DEFERRED …Run Code Online (Sandbox Code Playgroud) django ×3
cascade ×1
django-1.7 ×1
django-south ×1
many-to-many ×1
migration ×1
postgresql ×1
python-3.x ×1