Django--QuerySet--基础查询
创建模型类:
from django.db import models # Create your models here. class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=200) email = models.EmailField() def __str__(self): return self.name class Entry(models.Model): blog = models.ForeignKey(Blog, on_delete=models.CASCADE) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date = models.DateField() mod_date = models.DateField() authors = models.ManyToManyField(Author) n_comments = models.IntegerField() n_pingbacks = models.IntegerField() rating = models.IntegerField() def __str__(self): return self.headline
对数据库的迁移生成数据表:
| queryset_demo_author | | queryset_demo_blog | | queryset_demo_entry | | queryset_demo_entry_authors | # 在模型类中我们使用了 多对多所以系统会自动的为我们创建了一个表。 +-----------------------------+
queryset_demo_entry_authors表结构
mysql> desc queryset_demo_entry_authors;
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| entry_id | int(11) | NO | MUL | NULL | |
| author_id | int(11) | NO | MUL | NULL | |
+-----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
使用shell 进行对数据库的测试:
save()
(venv) lee@lee:~/PycharmProjects/Djdemo/djdemo$ python manage.py shell # 执行shell >>> from queryset_demo.models import Blog,Author,Entry # 导入所需要得类 >>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') # 创建对象 >>> b.save() # 存入数据库
对数据库进行查询:
mysql> select * from queryset_demo_blog; +----+--------------+------------------------------+ | id | name | tagline | +----+--------------+------------------------------+ | 1 | Beatles Blog | All the latest Beatles news. | +----+--------------+------------------------------+ 1 row in set (0.01 sec)
creat()
>>> crete_demo = Blog.objects.create(name='create_test',tagline='This is the wayof create') # 数据库得查询 mysql> select * from queryset_demo_blog; +----+--------------+------------------------------+ | id | name | tagline | +----+--------------+------------------------------+ | 1 | Beatles Blog | All the latest Beatles news. | | 2 | create_test | This is the wayof create | +----+--------------+------------------------------+
在数据库中已经存在的数据进行属性的修改:
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') >>> b.save() >>> b.name 'Beatles Blog' >>> b.name = 'change_new_name' # 对属性的值进行修改 >>> b.name 'change_new_name' >>> b.save() # 将修改的值存储到数据库中 mysql> select * from queryset_demo_blog; +----+-----------------+------------------------------+ | id | name | tagline | +----+-----------------+------------------------------+ | 1 | change_new_name | All the latest Beatles news. | | 2 | create_test | This is the wayof create | +----+-----------------+------------------------------+ 2 rows in set (0.00 sec)
注意:
1. 必须是在数据库中存在的数据。
2.当我们获取到属性的时候,进行修改后,必须执行save()方法才能重新的存入到数据库中。
ForeignKey字段的更新:
数据库文件的添加:
mysql> select * from queryset_demo_author; +----+-------+---------------+ | id | name | email | +----+-------+---------------+ | 1 | Tom | Tom@123.com | | 2 | Jone | jone@123.com | | 3 | Kevin | kevin@123.com | +----+-------+---------------+ 3 rows in set (0.00 sec) mysql> select * from queryset_demo_blog; +----+-----------------+------------------------------+ | id | name | tagline | +----+-----------------+------------------------------+ | 1 | change_new_name | All the latest Beatles news. | | 2 | create_test | This is the wayof create | | 3 | Cheddar Talk | One to Many test | | 4 | blog_3 | this is a test | +----+-----------------+------------------------------+ 4 rows in set (0.00 sec) mysql> select * from queryset_demo_entry; +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 2 | # 稍后我们将进行一个修改 +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ 1 row in set (0.00 sec)
对一对多的字段进行更新:
需求让Entry的表id = 1 的数据和 Blog id = 4 进行关联
>>> from queryset_demo.models import Blog,Entry,Author >>> entry = Entry.objects.get(pk=1) # 数据库中只有一条数据 >>> entry <Entry: python> # 返回一个对象 >>> entry.blog <Blog: create_test> # 通过属性获得我们关联的一个对象 >>> entry.blog.tagline 'This is the wayof create' # 获取关联对象的属性 >>> change_bolg_3 = Blog.objects.get(pk=4) # 创建我们想要更新的对象 >>> entry.blog = change_bolg_3 >>> entry.save() >>> entry.blog = change_bolg_3 # 跟新我们的关联 >>> entry.save() # 存储到数据库 对数据库查询验证 >>> entry = Entry.objects.get(pk=1) >>> entry <Entry: python> >>> entry.blog <Blog: blog_3> mysql> mysql> select * from queryset_demo_entry; +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ 1 row in set (0.00 sec)
ManyToManyField的更新:
需要注意的地方在创建模型类的时候我们知道当字段出现多对多的对应关系的时候那么djnago会在系统中给给我们默认的生成一张关系表。
>>> john = Author.objects.create(name="John") >>> paul = Author.objects.create(name="Paul") >>> entry.authors.add(john,paul) # 可以同时添加多个 entry中的authors字段 数据库中进行插叙 mysql> select * from queryset_demo_author; +----+-------+---------------+ | id | name | email | +----+-------+---------------+ | 1 | Tom | Tom@123.com | | 2 | Jone | jone@123.com | | 3 | Kevin | kevin@123.com | | 4 | Joe | | | 5 | John | | | 6 | Paul | | +----+-------+---------------+ 6 rows in set (0.00 sec) mysql> select * from queryset_demo_entry_authors; +----+----------+-----------+ | id | entry_id | author_id | +----+----------+-----------+ | 1 | 1 | 2 | | 2 | 1 | 4 | | 3 | 1 | 5 | | 4 | 1 | 6 | +----+----------+-----------+ 4 rows in set (0.00 sec)