文章目录

* 创建Question对象
<https://blog.csdn.net/baidu_39459954/article/details/90241261#Question_1>
* 查询创建的Question对象
<https://blog.csdn.net/baidu_39459954/article/details/90241261#Question_27>
* __ str __() 方法
<https://blog.csdn.net/baidu_39459954/article/details/90241261#___str_____52>
* 数据库查找API
<https://blog.csdn.net/baidu_39459954/article/details/90241261#API_80>
* 自定义方法 <https://blog.csdn.net/baidu_39459954/article/details/90241261#_107>
* 创建Choice对象
<https://blog.csdn.net/baidu_39459954/article/details/90241261#Choice_142>
*
[上一篇:Pycharm+Django手把手入门实战教程3-数据库配置](https://blog.csdn.net/baidu_39459954/article/details/90234659)

<https://blog.csdn.net/baidu_39459954/article/details/90241261#PycharmDjango3httpsblogcsdnnetbaidu_39459954articledetails90234659_190>
* 下一篇:Pycharm+Django手把手入门实战教程5-后台管理页面
<https://blog.csdn.net/baidu_39459954/article/details/90241261#PycharmDjango5_191>


<>创建Question对象

现在让我们进入交互式 Python 命令行,尝试一下 Django 为你创建的各种 API。通过以下命令打开 Python 命令行:
python manage.py <http://manage.py> shell
或者我们可以打开Pycharm的Tool->Python Console
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit
(AMD64)] on win32
Django 2.2.1

引入上一篇中我们创建的Choice和Question模型类:
>>> from polls.models import Choice, Question
查询Question中的内容,目前还是空:
>>> Question.objects.all() <QuerySet []>

创建一个新的Question,在Django中建议使用timezone.now()而不是datetime.datetime.now(),因此引入timezone:
>>> from django.utils import timezone >>> q = Question(question_text="What's
new?", pub_date=timezone.now())
使用save(),保存刚刚创建的Question进数据库:
>>> q.save()
查询数据库可以看到,数据已经保存:


<>查询创建的Question对象

ID是自动生成的,q.id可以查询当前的id号:
>>> q.id 1
通过python属性访问模型字段值:
Access model field values via Python attributes.
>>> q.question_text "What's new?" >>> q.pub_date datetime.datetime(2019, 5,
19, 16, 23, 13, 320097)

通过修改属性修改对应的值,然后再调用save():
>>> q.question_text = "What's up?" >>> q.save()
objects.all()显示数据库中所有的对象:
>>> Question.objects.all() <QuerySet [<Question: Question object (1)>]>
<>__ str __() 方法

但是输出结果 <Question: Question object (1)> 对于我们了解这个对象的细节没什么帮助。我们可以通过编辑 Question
模型的代码(位于 polls/models.py 中)来修复这个问题。给 Question 和 Choice 增加 __str __() 方法。
from django.db import models class Question(models.Model): question_text =
models.CharField(max_length=200) pub_date = models.DateTimeField('date
published') def __str__(self): return self.question_text class
Choice(models.Model): question = models.ForeignKey(Question,
on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes
= models.IntegerField(default=0) def __str__(self): return self.choice_text
重新打开Python Console,再次查询,确认我们加的__str __() 生效了:
>>> from polls.models import Choice, Question >>> Question.objects.all()
<QuerySet [<Question: What's up?>]>
<>数据库查找API

Django提供了丰富的数据库查找API,它完全由关键字参数驱动。
>>> Question.objects.filter(id=1) <QuerySet [<Question: What's up?>]> >>>
Question.objects.filter(question_text__startswith='What') <QuerySet [<Question:
What's up?>]>
获取今年发布的问题:
>>> from django.utils import timezone >>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year) <Question: What's up?>
如果请求不存在的ID,将引发异常:
>>> Question.objects.get(id=2) Traceback (most recent call last): ...
polls.models.Question.DoesNotExist: Question matching query does not exist.
通过主键查找是最常见的情况,因此Django提供了主键精确查找的快捷方式,以下内容与Question.objects.get(id = 1)相同。
>>> Question.objects.get(pk=1) <Question: What's up?>
<>自定义方法

给模型增加 __ str__() 方法是很重要的,这不仅仅能给你在命令行里使用带来方便,Django 自动生成的 admin 里也使用这个方法来表示对象。
注意:这些都是常规的 Python方法。让我们添加一个自定义的方法was_published_recently,这只是为了演示:
import datetime from django.db import models from django.utils import timezone
class Question(models.Model): question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') def __str__(self): return
self.question_text def was_published_recently(self): return self.pub_date >=
timezone.now() - datetime.timedelta(days=1) class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text =
models.CharField(max_length=200) votes = models.IntegerField(default=0) def
__str__(self): return self.choice_text
重新连接,查看我们新加的自定义方法是否生效:
>>> from polls.models import Choice, Question >>> q =
Question.objects.get(pk=1) >>> q.was_published_recently() True
<>创建Choice对象

给提出的问题添加几个选择。
The create call constructs a new Choice object, does the INSERT statement,
adds the choice to the set of available choices and returns the new Choice
object. Django creates a set to hold the “other side” of a ForeignKey relation

(例如问题的选择)可以通过API访问。
>>> q = Question.objects.get(pk=1)
显示q对象(主键为1)对应的choice,目前为空。
>>> q.choice_set.all() <QuerySet []>
创建三个Choice:
>>> q.choice_set.create(choice_text='Not much', votes=0) <Choice: Not much>
>>> q.choice_set.create(choice_text='The sky', votes=0) <Choice: The sky> >>> c
= q.choice_set.create(choice_text='Just hacking again', votes=0)
Choice对象具有对其相关Question对象的API访问权限。
>>> c.question <Question: What's up?>
反之亦然:Question 对象可以访问Choice对象。
>>> q.choice_set.all() <QuerySet [<Choice: Not much>, <Choice: The sky>,
<Choice: Just hacking again>]> >>> q.choice_set.count() 3
API会根据您的需要自动跟踪关系,使用双下划线来分隔关系,这可以像你想要的那样深入多层次; 没有限制。
查找pub_date在今年的任何问题的所有选择(可重用我们上面创建的’current_year’变量)。
>>> from django.utils import timezone >>> current_year = timezone.now().year
>>> Choice.objects.filter(question__pub_date__year=current_year) <QuerySet
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
使用delete()删除其中一个choice。
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking') >>>
c.delete()
查询数据库,发现对应的choice已删除


<>上一篇:Pycharm+Django手把手入门实战教程3-数据库配置
<https://blog.csdn.net/baidu_39459954/article/details/90234659>

<>下一篇:Pycharm+Django手把手入门实战教程5-后台管理页面

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信