increase
There are two ways to increase data.
-
save
By creating a model class object, execute the object's save () method and save it in the database.
from book.models import BookInfo,PeopleInfo book = BookInfo( name='python入门', pub_date='2010-1-1' ) book.save() book <BookInfo: python入门>
-
create
Save by model class.objects.create ().
PeopleInfo.objects.create( name='itheima', book=book ) <PeopleInfo: itheima>
modify
There are two ways to modify and update
-
save
Modify the properties of the model class object, and then execute the save () method
person = PeopleInfo.objects.get(name='itheima') person.name = 'itcast' person.save() person <PeopleInfo: itcast>
-
update
Using the model class .objects.filter (). Update () will return the number of affected rows
PeopleInfo.objects.filter(name='itcast').update(name='H_sen') 1
delete
There are two ways to delete
- Model class object delete
person = PeopleInfo.objects.get(name='H_sen')
person.delete()
(1, {'book.PeopleInfo': 1})
- Model class.objects.filter (). Delete ()
BookInfo.objects.filter(name='python入门').delete()
(1, {'book.BookInfo': 1, 'book.PeopleInfo': 0})
Inquire
Basic query
-
get Query a single result. If it does not exist, it will throw a model class. DoesNotExist exception.
-
all queries multiple results.
-
count The number of query results.
BookInfo.objects.get(id=1)
<BookInfo: 射雕英雄传>
BookInfo.objects.get(pk=2)
<BookInfo: 天龙八部>
BookInfo.objects.get(pk=20)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/python/.virtualenvs/py3_django_1.11/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/python/.virtualenvs/py3_django_1.11/lib/python3.5/site-packages/django/db/models/query.py", line 380, in get
self.model._meta.object_name
book.models.DoesNotExist: BookInfo matching query does not exist.
BookInfo.objects.all()
<QuerySet [<BookInfo: 射雕英雄传>, <BookInfo: 天龙八部>, <BookInfo: 笑傲江湖>, <BookInfo: 雪山飞狐>]>
BookInfo.objects.count()
4
Filter query
Implement where functions in SQL, including
-
filter out multiple results
-
Exclude the remaining results that meet the conditions
-
get filter a single result
For the use of filter conditions, the above three methods are the same, so only filter is used for explanation.
The expression syntax of the filter condition is as follows:
属性名称__比较运算符=值
# 属性名称和比较运算符间使用两个下划线,所以属性名不能包括多个下划线
查询编号为1的图书
查询书名包含'湖'的图书
查询书名以'部'结尾的图书
查询书名为空的图书
查询编号为1或3或5的图书
查询编号大于3的图书
查询1980年发表的图书
查询1990年1月1日后发表的图书
1. equal
exact: indicates judgment.
Example: Query book number 1.
BookInfo.objects.filter(id__exact=1)
可简写为:
BookInfo.objects.filter(id=1)
2. Fuzzy query
contains: whether it contains.
Note: If you want to include% without escaping, just write it directly.
Example: Query books whose book title contains 'Chuan'.
BookInfo.objects.filter(name__contains='传')
<QuerySet [<BookInfo: 射雕英雄传>]>
startswith, endswith: start or end with the specified value.
Example: Query books whose title ends with "部"
BookInfo.objects.filter(name__endswith='部')
<QuerySet [<BookInfo: 天龙八部>]>
以上运算符都区分大小写,在这些运算符前加上i表示不区分大小写,
如iexact、icontains、istartswith、iendswith.
3. Empty query
isnull: Whether it is null.
Example: Query books with empty book names.
BookInfo.objects.filter(name__isnull=True)
<QuerySet []>
4. Range query
in: whether it is included in the range.
Example: Query books with the number 1 or 3 or 5
BookInfo.objects.filter(id__in=[1,3,5])
<QuerySet [<BookInfo: 射雕英雄传>, <BookInfo: 笑傲江湖>]>
5. Comparison query
-
gt is greater than (greater then)
-
gte is greater than or equal to (greater then equal)
-
lt is less (less then)
-
lte is less than or equal to (less then equal)
Example: Query books with a number greater than 3
BookInfo.objects.filter(id__gt=3)
Operators that are not equal, use the exclude () filter.
Example: Query books with a number not equal to 3
BookInfo.objects.filter(id__gt=3)
<QuerySet [<BookInfo: 雪山飞狐>]>
6. Date query
year, month, day, week_day, hour, minute, second: operate on date-time type attributes.
Example: Query books published in 1980.
BookInfo.objects.filter(pub_date__year=1980)
<QuerySet [<BookInfo: 射雕英雄传>]>
Example: Query books published after January 1, 1990.
BookInfo.objects.filter(pub_date__gt='1990-1-1')
<QuerySet [<BookInfo: 笑傲江湖>]>