Python Shell 解释器下使用Django Model
2014-10-28 15:57
375 查看
sys.path.append('E:/Projects/DjangoProjects/myFirstSite')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myFirstSite.settings')
from books.models import *
print (Author.objects.all())
如下(http://stackoverflow.com/questions/8047204/django-script-to-access-model-objects-without-using-manage-py-shell):
Since Django 1.4 you should avoid using
Here is an example to access and modify your model:
Example model:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myFirstSite.settings')
from books.models import *
print (Author.objects.all())
如下(http://stackoverflow.com/questions/8047204/django-script-to-access-model-objects-without-using-manage-py-shell):
Since Django 1.4 you should avoid using
setup_environ(settings)(post by Melug) because it is deprecated. Use the following instead and you will be able to access your model
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings") # your imports, e.g. Django models from your_project_name.models import Location # From now onwards start your script..
Here is an example to access and modify your model:
if __name__ == '__main__': # e.g. add a new location l = Location() l.name = 'Berlin' l.save() # this is an example to access your model locations = Location.objects.all() print locations # e.g. delete the location berlin = Location.objects.filter(name='Berlin') print berlin berlin.delete()
Example model:
class Location(models.Model): name = models.CharField(max_length=100)
相关文章推荐
- 单独的 python 脚本文件使用 django 自带的 model
- 单独的 python 脚本文件使用 django 自带的 model
- 【Python】Django Model 怎么使用 UUID 作为主键?
- Python指南--使用Python解释器
- 关于使用Aptana+Pydev构建Python开发环境(Django)
- python 使用django post提交时出现 CSRF verification failed. Request aborted.
- Python-Django 使用 Hudson做CI服务器实践记
- 在django1.2+python2.7环境中使用send_mail发送邮件 推荐
- Python Tutorial 第二章 使用Python解释器
- Python 教程阅读笔记(一):使用解释器
- 使用Aptana+Pydev构建Python开发环境(Django)
- 使用Flex 4、Django、Python和PyAMF进行开发
- Django: show/log ORM sql calls from python shell - Stack Overflow
- 用Python编写shell脚本时经常使用的函数的介绍
- 使用 Django 和 Python 开发 Web 站点
- python/Django下读写文件,等待,调用shell命令等技术总结
- Django在使用mod_python+apache中配置VirtualHost的问题总结
- python下的web开发框架-Django,django模板的使用
- [Dynamic Language] Python Django: 模板引擎(4)在视图中使用模板和模板继承
- python(django) 使用PIL报错IOError at decoder jpeg not available