本文共 3301 字,大约阅读时间需要 11 分钟。
django迁移和创建
I’m working on an online shop for our local hackerspace . To implement it, we chose because I already know how to deal with Django and because it seems to be very customizeable.
我正在一家本地hackerspace 的在线商店中工作。 为了实现它,我们选择了因为我已经知道如何处理Django,并且因为它似乎非常可定制。
In it is mentioned that every shop needs a product class and a fulfillment partner. The proper way to implement that is to create a data migration.
在中提到每个商店都需要一个产品类别和一个履行合作伙伴。 正确的实现方法是创建数据迁移。
The docs link to django-south though, the 3rd party database migration library that has been superceded with Django’s own . So the instructions are not 100% clear anymore. After some failed attempts, I worked out how to properly create data migrations for Oscar.
文档链接到django-south,这是已被Django自己的取代的第三方数据库迁移库。 因此,说明不再是100%清晰的了。 经过几次失败的尝试后,我研究出如何为Oscar正确创建数据迁移。
First of all, you need a django app to contain your data migrations. If you haven’t created one yet, do it now:
首先,您需要一个django应用程序来包含您的数据迁移。 如果尚未创建,请立即执行以下操作:
$ ./manage.py startapp data
Then add it to INSTALLED_APPS in your settings.py.
然后将其添加到settings.py中的 INSTALLED_APPS中。
I’d recommend to also create an initial migration, so that your data migration isn’t the first one.
我建议您还创建一个初始迁移,以便您的数据迁移不是第一个。
$ ./manage.py makemigrations --empty data
Now you can create an empty data migration. Django’s migration framework doesn’t differentiate between data migrations and schema migrations anymore, so you can just use the --empty argument:
现在,您可以创建一个空数据迁移。 Django的迁移框架不再区分数据迁移和模式迁移,因此您可以使用--empty参数:
$ ./manage.py makemigrations --empty data
It is recommended to create initial instances of the following models:
建议创建以下模型的初始实例:
Additionally, if you don’t import countries with ./manage.py oscar_populate_countries, you need to create a Country instance.
此外,如果您不使用./manage.py oscar_populate_countries导入国家/地区 ,则需要创建一个Country实例。
In order to create model instances for other apps, you need them as dependencies. All these models are created in the corresponding initial migration, therefore you can use the following dependency list:
为了为其他应用程序创建模型实例,您需要将它们作为依赖项。 所有这些模型都是在相应的初始迁移中创建的,因此可以使用以下依赖项列表:
dependencies dependencies = = [ [ # Your own app # Your own app (( 'data''data' , , '0001_initial''0001_initial' ), ), # Oscar apps # Oscar apps (( 'catalogue''catalogue' , , '0001_initial''0001_initial' ), ), (( 'partner''partner' , , '0001_initial''0001_initial' ), ), (( 'address''address' , , '0001_initial''0001_initial' ),),]]
For each of these model types we’ll create a function that can be run with the migrations.RunPython operation. Here’s the example for the country:
对于每种模型类型,我们将创建一个可与migrations.RunPython操作一起运行的函数。 这是该国家/地区的示例:
It’s important that you use address as app label and not oscar.apps.address or oscar.apps.address.models.
请务必将地址用作应用标签,而不要使用oscar.apps.address或oscar.apps.address.models 。
Add matching functions for all other data types.
为所有其他数据类型添加匹配功能。
That’s it. If you want to see an example of such a data migration, see .
而已。 如果要查看此类数据迁移的示例,请参见 。
翻译自:
django迁移和创建
转载地址:http://opqwd.baihongyu.com/