django迁移和创建_创建Django Oscar初始数据迁移

django迁移和创建

I’m working on an online shop for our local hackerspace Coredump. To implement it, we chose django-oscar because I already know how to deal with Django and because it seems to be very customizeable.

我正在一家本地hackerspace Coredump的在线商店中工作。 为了实现它,我们选择了django-oscar,因为我已经知道如何处理Django,并且因为它似乎非常可定制。

In the docs 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 migration framework. 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正确创建数据迁移。

1.创建一个Django应用 (1. Create a Django app)

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

2.创建数据迁移 (2. Create a data migration)

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

3.查找依赖项 (3. Find dependencies)

It is recommended to create initial instances of the following models:

建议创建以下模型的初始实例:

  • Product Category
  • Product Class
  • Fulfillment Partner
  • Partner Address
  • 产品分类
  • 产品类别
  • 履行合作伙伴
  • 合作伙伴地址

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' ),
),
]
]

4.编写RunPython函数 (4. Write RunPython functions)

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.addressoscar.apps.address.models

Add matching functions for all other data types.

为所有其他数据类型添加匹配功能。

5.例子 (5. Example)

That’s it. If you want to see an example of such a data migration, see https://github.com/coredump-ch/shop/blob/master/apps/data/migrations/0002_product_types_and_partners.py.

而已。 如果要查看此类数据迁移的示例,请参见https://github.com/coredump-ch/shop/blob/master/apps/data/migrations/0002_product_types_and_partners.py

翻译自: https://www.pybloggers.com/2015/03/creating-django-oscar-initial-data-migration/

django迁移和创建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值