zoukankan      html  css  js  c++  java
  • 模型二

    基本的数据访问   直接上代码吧  比较简单的操作。

    >>> 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()
  • 相关阅读:
    fluent/starccm/商业CFD软件中残差的概念
    windows 下用命令来操作定时任务
    selenium下打开Chrome报错解决
    TypeError: a bytes-like object is required, not 'str'
    Cannot redeclare class phpmailerException
    linux freetds无法构建错误:为--with-tdsver:8.0指定的值无效
    Apache 修改端口号
    php 二维数组按照某个键排序
    php 计算 距离
    pymysql 读取数据库没有字段
  • 原文地址:https://www.cnblogs.com/A-FM/p/6399239.html
Copyright © 2011-2022 走看看