models.py

import datetime
from django.db import models
from django.utils import timezone class Question(models.Model):
def __str__(self):
return self.question_text def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1) question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') class Choice(models.Model):
def __str__(self):
return self.choice_text
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

python shell

from polls.models import Choice, Question
from django.utils import timezone q = Question(question_text="What's new?", pub_date=timezone.now())
q.save() q.id #输出:1
q.question_text #输出:What's new?
q.pub_date #输出:datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>) q.question_text = "What's up?"
q.save() Question.objects.all() #输出:<QuerySet [<Question: What's up?>]>
Question.objects.filter(id=1) #输出:<QuerySet [<Question: What's up?>]>
Question.objects.filter(question_text__startswith = 'What') #输出:<QuerySet [<Question: What's up?>]> Question.objects.get(pub_date__year = timezone.now().year) #输出:<Question: What's up?>
Question.objects.get(id=2) #输出:报错,因为只有1条记录
Question.objects.get(pk=1) #输出:<Question: What's up?>
q = Question.objects.get(pk=1)
q.was_published_recently() #输出:True q = Question.objects.get(pk=1)
q.choice_set.all() #输出:<QuerySet []> 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)
c.question #输出:<Question: What's up?>
q.choice_set.all()
#输出:<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
q.choice_set.count() #输出:3 Choice.objects.filter(question__pub_date__year=current_year)
#输出:<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]> c = q.choice_set.filter(choice_text__startswith='Just hacking')
c.delete() #删除该记录

models.py相关API的更多相关文章

  1. django模型models.py文件内容理解

    首先,要理解这句话:模型是你的数据的唯一的.权威的信息源.它包含你所存储数据的必要字段和行为.通常,每个模型对应数据库中唯一的一张表 基础:每个模型都是django.db.models.Model的一 ...

  2. Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据

    基于之前的项目代码来编写 Python Django CMDB项目实战之-1如何开启一个Django-并设置base页index页文章页面 现在我们修改一个文章列表是从数据库中获取数据, 下面我们就需 ...

  3. 第三百七十三节,Django+Xadmin打造上线标准的在线教育平台—创建用户app,在models.py文件生成3张表,用户表、验证码表、轮播图表

    第三百七十三节,Django+Xadmin打造上线标准的在线教育平台—创建用户app,在models.py文件生成3张表,用户表.验证码表.轮播图表 创建Django项目 项目 settings.py ...

  4. 第三百零八节,Django框架,models.py模块,数据库操作——链表结构,一对多、一对一、多对多

    第三百零八节,Django框架,models.py模块,数据库操作——链表结构,一对多.一对一.多对多 链表操作 链表,就是一张表的外键字段,连接另外一张表的主键字段 一对多 models.Forei ...

  5. 某音乐类App评论相关API的分析及SQL注入尝试

    关键字:APIfen.工具使用.sql注入 涉及工具/包:Fiddler.Burpsuite.Js2Py.Closure Compiler.selenium.phantomjs.sqlmap 摘要: ...

  6. TensorFlow - 相关 API

    来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...

  7. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  8. 3-app应用操作——Models.py和字段类型

    Models.py定义 每一个数据表对应一个model定义,model之间和java一样可以相互之间继承.所有的model都必须继承 from django.db import models#或间接继 ...

  9. OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)

    OpenGL FrameBufferCopy相关Api比较 glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D 标题所述 ...

随机推荐

  1. 基于Python和Xtrbackup的自动化备份与还原实现

    xtrabackup是一个MySQL备份还原的常用工具,实际使用过程应该都是shell或者python封装的自动化脚本,尤其是备份.对还原来说,对于基于完整和增量备份的还原,还原差异备份需要指定增量备 ...

  2. Redis & memcached PK

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  3. 一个MongoDB索引走偏的案例及探究分析

    接业务需求,有一个MongoDB的简单查询,太耗时了,执行了 70S 左右,严重影响用户的体验.. 查询代码主要如下: db.duoduologmodel.find({"Tags.SN&qu ...

  4. 为什么有的插件安装需要用Vue.use()方法

    问题 相信很多人在用Vue使用别人的组件时,会用到 Vue.use() .例如:Vue.use(VueRouter).Vue.use(MintUI).但是用 axios时,就不需要用 Vue.use( ...

  5. (day69)axios、配置ElementUI、配置jQuery和Bootstrap、Django中的CORS问题

    目录 一.Vue的ajax插件:axios 二.Django中的CORS跨域问题 (一)同源策略 (二)解决方式(cors模块) 三.Vue配置ElementUI 四.Vue配置jQuery和Boot ...

  6. PHPStorm 初遇 Xdebug (xdebug代码调试及性能分析)

    centos 7 下PHP7安装xdebug # 下载xdebug wget https://xdebug.org/files/xdebug-2.7.2.tgz # 解压 tar -xf xdebug ...

  7. JavaScript Array filter() 方法

    JavaScript Array filter() 方法 var ages = [32, 33, 16, 40]; function checkAdult(age) { return age > ...

  8. vuex动态引入store modules

    主要解决的问题每次建一个module需要自己去主index.js里面去注册 为了偷懒,也为了避免团队开发时同时对index.js 进行修改引发冲突 所以在index.js中 动态的对子目录和模块进行注 ...

  9. 从web到游戏,走出舒适区

    最近很久没有更新博客了,实在太忙.因为在这段时间里我做了一个改变了我现在职业生涯的一个决定,而我现在正在为这个决定而加倍的努力付出. 我认为我还是有必要把这个比较重要的节点记录下来,我也是第一次在自己 ...

  10. JavaScript-三种弹窗方式

    0918自我总结 JavaScript-三种弹窗方式 一.alert 带内容的弹框 用法: <script> alert('弹窗显示的内容') //会弹出框没有点确定不会执行下面的代码会发 ...