ipython的一个扩展,有助于在交互式会话中定义django的模型。
django-orm-magic的Python项目详细描述
author: | Martín Gaitán <gaitan@gmail.com> |
---|---|
homepage: | https://github.com/mgaitan/django-orm-magic |
documentation: | see this notebook |
在ipython单元中定义django模型并动态使用它们。 让魔法来完成无聊的部分。
django orm不是独立使用的。即使是一个小例子,你也需要配置一个数据库,创建一个应用程序,等等。这个神奇的自动处理,然后将每个模型导入到你的交互会话中。
安装
您可以通过pip安装或升级
pip install -U django-orm-magic
或者直接从存储库中使用%install\u extmagic命令:
In[1]: %install_ext https://raw.github.com/mgaitan/django-orm-magic/master/django_orm_magic.py
基本用法
一旦安装好,就可以用%load_ext django_orm_magic加载它。然后在以cell magic%%django_orm开头的单元格中定义模型。 例如:
In[2]: %load_ext django_orm_magic In[3]: %%django_orm from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
一切都结束了。每个模型都在当前路径中名为db.sqlite的sqlite数据库中同步,并自动导入:
In[4]: Poll.objects.all() Out[4]: [] In[5]: from django.utils import timezone p = Poll(question="What's new?", pub_date=timezone.now()) p.save()
有关详细信息,请参见documentation。
另一个例子见here
更改日志
0.3.1/2014-01-21
- 修复错误#4 (现在通过pip works安装)
0.3/2013-12-14
- 添加了%django_设置魔术
- 添加了教程笔记本
0.2/2013-12-11
- 将代码移到github。
- 在PYPI中打包并注册
0.1/2013-10-29
- 首次公开发行(作为gist)