我应该怎样为我的Djangowin7默认程序设置以南设置south

I recently switched from Django 1.6 to 1.7, and I began using migrations (I never used South).
Before 1.7, I used to load initial data with a fixture/initial_data.json file, which was loaded with the python manage.py syncdb command (when creating the database).
Now, I started using migrations, and this behavior is deprecated :
If an application uses migrations, there is no automatic loading of fixtures.
Since migrations will be required for applications in Django 2.0, this behavior is considered deprecated. If you want to load initial data for an app, consider doing it in a data migration.
does not have a clear example on how to do it, so my question is :
What is the best way to import such initial data using data migrations :
Write Python code with multiple calls to mymodel.create(...),
Use or write a Django function () to load data from a JSON fixture file.
I prefer the second option.
I don't want to use South, as Django seems to be able to do it natively now.
解决方案 Assuming you have a fixture file in &yourapp&/fixtures/initial_data.json
Create your empty migration:
In Django 1.7:
python manage.py makemigrations --empty &yourapp&
In Django 1.8+, you can provide a name:
python manage.py makemigrations --empty &yourapp& --name load_intial_data
Edit your migration file &yourapp&/migrations/0002_auto_xxx.py
2.1. Custom implementation, inspired by Django' loaddata (initial answer):
from sys import path
from django.core import serializers
fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
fixture_filename = 'initial_data.json'
def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()
def unload_fixture(apps, schema_editor):
"Brutally deleting all entries for this model..."
MyModel = apps.get_model("yourapp", "ModelName")
MyModel.objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
('yourapp', '0001_initial'),
operations = [
migrations.RunPython(load_fixture, reverse_code=unload_fixture),
2.2. A simpler solution for load_fixture (per @juliocesar's suggestion):
from django.core.management import call_command
fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
fixture_filename = 'initial_data.json'
def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
call_command('loaddata', fixture_file)
Useful if you want to use a custom directory.
2.3. Simplest: calling loaddata with app_label will load fixtures from the &yourapp&'s fixtures dir automatically :
from django.core.management import call_command
fixture = 'initial_data'
def load_fixture(apps, schema_editor):
call_command('loaddata', fixture, app_label='yourapp')
If you don't specify app_label, loaddata will try to load fixture filename from all apps fixtures directories (which you probably don't want).
python manage.py migrate &yourapp&
本文地址: &
我最近从Django 1.6切换到1.7,我开始使用迁移(我从未使用过South)。
在1.7之前,我曾经加载初始数据 fixture / initial_data.json 文件,它加载了 python manage.py syncdb 命令(创建数据库时) 。
现在,我开始使用迁移,这种行为已被弃用:
如果应用程序使用迁移,则不会自动加载固定装置。 由于Django 2.0中的应用程序需要迁移,所以这种行为被认为是不推荐的。如果要加载应用程序的初始数据,请考虑在数据迁移中进行。 ()
不有一个明确的例子,如何做,所以我的问题是:
使用数据迁移导入这些初始数据的最好方式是什么?
将多个调用的Python代码写入 mymodel.create(...),
使用或编写Django功能()从JSON夹具文件加载数据。
我更喜欢第二个选项。 p>
我不知道t使用南方,因为Django似乎现在可以自己做。
解决方案 假设你有一个夹具文件在& yourapp& /fixtures/initial_data.json
创建您的空迁移:
在Django 1.7中:
python manage.py makemigrations - 空& yourapp&
在Django 1.8+中,您可以提供一个名称:
python manage.py makemigrations --empty& yourapp& --name load_intial_data
编辑您的迁移文件& yourapp& /migrations/0002_auto_xxx.py
2.1。自定义实现,灵感来自Django' loaddata (初始答案):
从sys import path导入os
from django.core import serializers
fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__ file__) ,'../fixtures')) fixture_filename ='initial_data.json'
def load_fixture(apps,schema_editor): fixture_file = os.path.join(fixture_dir,
fixture = open(fixture_file,'rb') objects = serializers.deserialize('json',fixture,ignorenonexistent = True)对象中的obj: obj.save() fixture.close()
def unload_fixture(apps,schema_editor):“严格删除此模型的所有条目...”
MyModel = apps.get_model(“yourapp”,“ModelName”) MyModel.objects.all()。delete()
class Migration(migrations.Migration ): 依赖关系= [('yourapp ,'0001_initial'),]
operations = [ migrations.RunPython(load_fixture,reverse_code = unload_fixture),]
load_fixture (per @ juliocesar的建议)的一个更简单的解决方案:
from django.core.management import call_command
fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__ file__),'../fixtures')) fixture_filename ='initial_data.json'
def load_fixture(apps,schema_editor): fixture_file = os.path.join(fixture_dir,fixture_filename) call_command('loaddata',fixture_file )
如果要使用自定义目录,则很有用
2.3。调用 loaddata 与 app_label 将从中加载灯具 & yourapp& 的 fixtures
dir automatically:
from django.core.management import call_command
fixture ='initial_data'
def load_fixture(apps,schema_editor): call_command('loaddata' ,夹具,app_label ='yourapp')
如果不指定 app_label ,loaddata将尝试从所有应用程序夹具目录中加载 fixture
filename(你可能不会
python manage.py migrate& yourapp&
本文地址: &
扫一扫关注官方微信Django-South介绍 | the5fire的技术博客
Django-South介绍
Django-south是一个一直挂在耳边,但是没有尝试过的django的插件。这次项目用到,刚好补充一下。
什么是Django-South?
South是方便Django应用进行数据库迁移/变动的这么个应用。它的宗旨是提供一个简单,稳定和数据库独立迁移层,以此来摒弃那些随着时间变化model的schema发生变化所带来的麻烦。
上面是官方大致翻译,通俗来讲,就是帮你把model中字段的变化,同步到数据库的表中。
为什么需要它?
写过Django项目的同学,必然会遇到这个问题:定义好model之后,开发业务功能,不断的测试,发现model要改,怎么办? python manage.py sqlall 出来新添加的model中的字段,然后参照着到数据库通过 alter 修改表。
这样的变更是个细心的工作,一不小心,你的字段名写错,或者类型写错,然后你上线了,网站挂了。
因此需要这么样的一个工具,来弥补Django只能在第一次创建表的不足。South会被加到Django之后的版本中:
如何使用Django-South?
一个好的程序使用起来必定是简单的,South和它的宗旨一样,使用简单。只需要简单几步,针对已经建好model和创建完表的应用。
安装South到install_app中
然后就是几个命令:
# 第一次使用要执行前两条
python manage.py schemamigration &app& --initial
python manage.py migrate &app& --fake
python manage.py schemamigration &app& --auto
python manage.py migrate &app&
当然,South的功能不止这些,有兴趣或者需要的还是老实去看官方文档。
----EOF-----
微信公众号:码农悟凡
别人正在读
【上一篇】
【下一篇】
Please enable JavaScript to view the
访问手机版
小额赞助博主
觉得此博客对你有帮助,支付宝扫码赞助
你看的这个站点托管在腾讯云之上
其他分类:(小白变大牛)
(小白变大牛)
第三方登录:他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Django模版输出xml格式的方法 - 为程序员服务
为程序员服务
Django模版输出xml格式的方法
return render_to_response('user_role/api_user.xml',mimetype=&application/xml&)
您可能的代码
相关聚客文章
荣誉:1682
相关专栏文章}

我要回帖

更多关于 屏幕保护程序怎么设置 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信