博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django迁移和创建_创建Django Oscar初始数据迁移
阅读量:2526 次
发布时间:2019-05-11

本文共 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正确创建数据迁移。

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 .

而已。 如果要查看此类数据迁移的示例,请参见 。

翻译自:

django迁移和创建

转载地址:http://opqwd.baihongyu.com/

你可能感兴趣的文章
经典排序算法回顾:选择排序,快速排序
查看>>
BZOJ2213 [Poi2011]Difference 【乱搞】
查看>>
c# 对加密的MP4文件进行解密
查看>>
AOP面向切面编程C#实例
查看>>
Win form碎知识点
查看>>
避免使用不必要的浮动
查看>>
第一节:ASP.NET开发环境配置
查看>>
sqlserver database常用命令
查看>>
rsync远程同步的基本配置与使用
查看>>
第二天作业
查看>>
访问属性和访问实例变量的区别
查看>>
Spring MVC 异常处理 - SimpleMappingExceptionResolver
查看>>
props 父组件给子组件传递参数
查看>>
【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
查看>>
十二种获取Spring的上下文环境ApplicationContext的方法
查看>>
UVA 11346 Probability 概率 (连续概率)
查看>>
linux uniq 命令
查看>>
Openssl rand命令
查看>>
HDU2825 Wireless Password 【AC自动机】【状压DP】
查看>>
BZOJ1015: [JSOI2008]星球大战starwar【并查集】【傻逼题】
查看>>