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. SolrCloud 高可用集群搭建

    1.1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...

  2. node_modules/.bin/babel : 无法加载文件 D:\node\node_project\es6\node_modules\.bin\babel.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.co m/fwlink/?LinkID=135170 中的 about_Execution_Policies。

    刚入门es6,遇到上面问题,然后 解决方案: 以管理员身份运行vs code执行:get-ExecutionPolicy,显示Restricted,表示状态是禁止的执行:set-ExecutionPo ...

  3. 简单搭建docker registry

    已知信息: 服务端IP:192.168.7.2xx 客户端IP:192.168.7.1xx 服务端: docker registry中镜像本地映射地址:/Users/dockergit/private ...

  4. git 分布式控制版本管理器(上)

    git的作用: 1.更方便的存储版本 2.恢复之前的版本 3.更方便的对比 4.协同合作 下载地址git官网: https://git-scm.com/ 可自选自己电脑的操作系统 安装: 一路next ...

  5. IntersectionObserver API,观察元素是否进入了可视区域

    网页开发时,常常需要了解某个元素是否进入了"视口"(viewport),即用户能不能看到它. 上图的绿色方块不断滚动,顶部会提示它的可见性. 传统的实现方法是,监听到scroll事 ...

  6. (绿色)修正版gooflow流程解决方案(源码分享+在线演示+UI地址下载)

    gooflow出现挖矿机木马,请勿随意去其他网站下载!!! 一.功能简介 gooflow功能清单1.自定义流程绘制2.自定义属性添加3.支持3种步骤类型普通审批步骤自动决策步骤手动决策步骤 4.决策方 ...

  7. 《java编程思想(第四版)》第一二章学习笔记

    目录 一.Introduction 1.抽象过程 2.面向对象语言(包括Java)的五个基本特性 3.每个对象都提供服务 4.public.private.protected三者的区别 5.Java的 ...

  8. Vue小练习 03

    """ 1.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/tv/tv1.jpg', title: 'tv1'}, ...

  9. SpringBoot2.0 整合 FastDFS 中间件,实现文件分布式管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.FastDFS简介 1.FastDFS作用 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步 ...

  10. Comprehensive Tutorial 综合教程(MainDemo应用程序)

    Follow this tutorial to create a simple application used to store contacts and other related objects ...