创建数据
创建一个book表进行演示
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(verbose_name='书名', max_length=11)
author = models.CharField(verbose_name='作者', max_length=11)
age = models.IntegerField(verbose_name='年龄')
class Meta:
# 数据库表名
db_table = 'book_test'
数据库中查看表
插入数据
查看数据库数据
get和filter区别
1.返回值
- get的返回值是一个定义的model类的实例,即对象
- filter的返回值是一个QuerySet的集合对象,可使用迭代或者遍历,切片等,但是不等于list类型(使用一定要注意)
2.异常
- get:只有一条记录返回的时候才正常。所以get多用于查询主键字段或者具有唯一性约束的字段,当有多条记录或者没有记录返回时,使用get均会抛出异常。
- filter:没有记录、有一条或者多条记录返回均不会抛出异常。(没有记录的时候,返回值是一个空集合)
使用get查询数据:
1.返回多条记录
In [15]: book = Book.objects.get(age=38)
---------------------------------------------------------------------------
MultipleObjectsReturned Traceback (most recent call last)
<ipython-input-15-231f3a63dc48> in <module>
----> 1 book = Book.objects.get(age=38)
D:\anaconda3\lib\site-packages\django\db\models\manager.py in manager_method(self, *args, **kwargs)
83 def create_method(name, method):
84 def manager_method(self, *args, **kwargs):
---> 85 return getattr(self.get_queryset(), name)(*args, **kwargs)
86 manager_method.__name__ = method.__name__
87 manager_method.__doc__ = method.__doc__
D:\anaconda3\lib\site-packages\django\db\models\query.py in get(self, *args, **kwargs)
440 'get() returned more than one %s -- it returned %s!' % (
441 self.model._meta.object_name,
--> 442 num if not limit or num < limit else 'more than %s' % (limit - 1),
443 )
444 )
MultipleObjectsReturned: get() returned more than one Book -- it returned 2!
2.没有返回数据时
In [16]: book = Book.objects.get(age=30)
---------------------------------------------------------------------------
DoesNotExist Traceback (most recent call last)
<ipython-input-16-9ea71ada6640> in <module>
----> 1 book = Book.objects.get(age=30)
D:\anaconda3\lib\site-packages\django\db\models\manager.py in manager_method(self, *args, **kwargs)
83 def create_method(name, method):
84 def manager_method(self, *args, **kwargs):
---> 85 return getattr(self.get_queryset(), name)(*args, **kwargs)
86 manager_method.__name__ = method.__name__
87 manager_method.__doc__ = method.__doc__
D:\anaconda3\lib\site-packages\django\db\models\query.py in get(self, *args, **kwargs)
435 raise self.model.DoesNotExist(
436 "%s matching query does not exist." %
--> 437 self.model._meta.object_name
438 )
439 raise self.model.MultipleObjectsReturned(
DoesNotExist: Book matching query does not exist.
使用filter查询时
返回多条数据
In [18]: book = Book.objects.filter(age=38)
In [19]: book
Out[19]: <QuerySet [<Book: Book object (2)>, <Book: Book object (4)>]>
In [20]: for item in book:
...: print(item.title,item.author,item.age)
...:
Django jack 38
Django bob 38
没有返回数据
In [21]: book = Book.objects.filter(age=30)
In [22]: book
Out[22]: <QuerySet []>
3.对表的修改
(1)user.update()方法
这种方法只能配合filter方法进行使用:
In [23]: book = Book.objects.filter(age=38)
In [24]: book = Book.objects.filter(title='python')
In [25]: book.update(age=20)
Out[25]: 1
使用get方法会报错:
In [26]: book = Book.objects.get(title='python')
In [27]: book.update(age=23)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-27-8f910998b099> in <module>
----> 1 book.update(age=23)
AttributeError: 'Book' object has no attribute 'update'
(2)user.xx = xx, user.save()
In [30]: book = Book.objects.get(title='python') In [31]: book.age = 25 In [32]: book.save()