首页 > 代码库 > 模型二
模型二
基本的数据访问 直接上代码吧 比较简单的操作。
>>> from book.models import Publisher >>> p1 = Publisher(name=‘Apress‘, address=‘2855 Telegraph Avenue‘,city=‘Berkeley‘, state_province=‘CA‘, country=‘U.S.A.‘,website=‘http://www.apress.com/‘) >>> p1.save() >>> p2 = Publisher(name="O‘Reilly", address=‘10 Fawcett St.‘,city=‘Cambridge‘, state_province=‘MA‘, country=‘U.S.A.‘,website=‘http://www.oreilly.com/‘) >>> p2.save() >>> publisher_list = Publisher.objects.all() >>> publisher_list <QuerySet [<Publisher: Publisher object>, <Publisher: Publisher object>]> >>> type(publisher_list) <class ‘django.db.models.query.QuerySet‘> >>> for i in publisher_list: ... i ... <Publisher: Publisher object> <Publisher: Publisher object> >>> for i in publisher_list: ... i.name ... ‘Apress‘ "O‘Reilly" >>> Publisher.objects.create(name="O‘Yuqn", address=‘11 Fawcett St.‘,city=‘Cambridge‘, state_province=‘MA‘, country=‘U.S.A.‘,website=‘http://www.oreilly.com/‘)<Publisher: Publisher object> >>> Publisher.objects.create(name="O‘Y11uqn", address=‘11 Fa11wcett St.‘,city=‘11Cambridge‘, state_province=‘11MA‘, country=‘U.111S.A.‘,website=‘http://www.orei11lly.com/‘) <Publisher: Publisher object> >>> publisher_list = Publisher.objects.all() >>> for i in publisher_list: ... i.name ... ‘Apress‘ "O‘Reilly" "O‘Yuqn" "O‘Y11uqn"
如果我们要删除对象的话,删除数据库中的对象只需调用该对象的delete()方法即可:
>>> p = Publisher.objects.get(name="O‘Reilly") >>> p.delete() >>> Publisher.objects.filter(country=‘USA‘).delete() >>> Publisher.objects.all().delete()
更新对象,很简单,先获取对象然后将其当做一般的变量进行赋值即可。当然还是要保存的。
>>> p.name = ‘Apress Publishing‘ >>> p.save()
模型二
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。