一:时区的转换

1、navie 时间 和 aware 时间

  • navie 时间:不知道自己的时间表示的是哪个时区;
  • aware 时间:知道自己的时间表示的是哪个时区。

2、pytz 库:用来处理时区的库,会经常更新时区数据,安装 django 时默认安装;(或通过 pip install pytz 安装)

3、astimezone 方法:将一个时区的时间转换为另一个时区的时间,只能被 aware 类型的时间调用,不能被 navie 类型的时间调用

 #在Linux环境下的操作;window下使用navie类型转换不会报错!
import pytz
from datetime import datetime now = datetime.now() # 这是一个 navie 类型的时间
# >>> datetime.datetime(2019, 2, 26, 20, 58, 32, 17072)
utc_timezone = pytz.timezone('UTC') # 定义UTC的时区对象
utc_now = now.astimezone(utc_timezone) # 将当前时间转换为UTC时区的时间
# >>>ValueError: astimezone() cannot be applied to a navie datetime
# 会抛出一个异常,因为navie 类型的时间不能调用astimezone # 使用replace 可将时间的某些属性进行更改,换成aware 类型后可正常转换;
now = now.replace(tzinfo=pytz.timezone('Asia/Shanghai'))
# >>> datetime.datetime(2019, 2, 26, 20, 58, 32, 17072, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>)
utc_now = now.astimezone(utc_timezone)
# >>> datetime.datetime(2019, 2, 26, 12, 52, 32, 17072, tzinfo=<UTC>)

二:orm_intro_demo项目:

models.py:

 class Article(models.Model):
#自己定义的Field作为主键时,必须设置primary_key=True;
id = models.AutoField(primary_key=True)
#使用可以为null的BooleanField时,用NullBooleanField代替;
removed = models.NullBooleanField()
# CharField:超过254个字符时,使用TextField();
# auto_now_add:在第一次添加数据时会自动获取当前时间;
# auto_now:每次这个对象调用save()方法时都会将当前时间更新;
create_time = models.DateTimeField(auto_now=True)
# 创建时间
# 更新时间 # 1、null值:age可为空(null值),username则是一个为空的字符串而不是null值。
class Author(models.Model):
# null默认为False,没指定值时username在数据库中显示为空字符串,而不是(null);
username = models.CharField(max_length=100)
# null为True时,如果没有为age指定值则数据库中将显示(null);
age = models.IntegerField(null=True,db_column='author_age',default=0)
create_time = models.DateTimeField(auto_now=now)
# unique:在表中这个字段的值是否唯一。一般是设置手机号码 / 邮箱等。
telephone = models.CharField(max_length=11,unique=True,null=0) def __str__(self):
return "<Author id:%s,create_tiem:%s>)" % (self.id,self.create_time)
# 2、
class Meta:
#修改数据库中的表名
db_table = 'author'
#根据时间,id 的顺序来显示数据,- :为相反顺序;
ordering = ['-create_time','id']

views.py:

 # 1、null值的映射
def unique(request):
# 使用了unique 后每次添加到数据库中telephone的值都得变,它具有唯一性;
author = Author(telephone=6)
author.save()
return HttpResponse('Success') # 2、打印数据的显示顺序
def order_view(request):
authors = Author.objects.all()
for author in authors:
print(author)
return HttpResponse('success')

三:外键 (orm_ForeignKey项目)

models.py:

 # 外键
class Category(models.Model):
name = models.CharField(max_length=100) class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
# 外键模型:class Foreign(to,on_delete,**options);
# to:表引用的模型;
# on_delete:后加CASCAEDE、SER_NULL等方法表外键引用的模型数据被删时的处理方法;
# category = models.ForeignKey('Category',on_delete=models.CASCADE)
# SET_DEFAULT:引用的数据被删除时可调用默认的值
category = models.ForeignKey('Category',on_delete=models.SET_DEFAULT,null=True,default=Category.objects.get(pk=1))
# app.models_name :应用名.模型名
# 引用的模型不在同个App下时使用,同App下可直接用模型名或self表示;
# author = models.Foreign('article.Category'.on_delete=CASCADE)

views.py:

from django.shortcuts import render
from .models import Article,Category
from django.http import HttpResponse def index(request):
category = Category(name='最新文章')
category.save()
article = Article(title='百年孤独',content='这本书不错...')
article.category = category
article.save()
return HttpResponse('success') # 使用CASCADE级联删除整条数据;
def author(request):
author = Category.objects.get(pk=4)
author.delete()
return HttpResponse('delete success')

Field笔记的更多相关文章

  1. Django笔记&教程 4-2 模型(models)中的Field(字段)

    Django 自学笔记兼学习教程第4章第2节--模型(models)中的Field(字段) 点击查看教程总目录 参考:https://docs.djangoproject.com/en/2.2/ref ...

  2. ExtJS笔记 Field

    Fields are used to define what a Model is. They aren't instantiated directly - instead, when we crea ...

  3. Superpixel Based RGB-D Image Segmentation Using Markov Random Field——阅读笔记

    1.基本信息 题目:使用马尔科夫场实现基于超像素的RGB-D图像分割: 作者所属:Ferdowsi University of Mashhad(Iron) 发表:2015 International ...

  4. openerp学习笔记 domain 增加扩展支持,例如支持 <field name="domain">[('type','=','get_user_ht_type()')]</field>

    示例代码1,ir_action_window.read : # -*- coding: utf-8 -*-from openerp.osv import fields,osv class res_us ...

  5. SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-006- 使用thymeleaf(TemplateResolver、SpringTemplateEngine、ThymeleafViewResolver、th:include、th:object、th:field="*{firstName}")

    一.在Spring中使用thymeleaf的步骤 1.配置 In order to use Thymeleaf with Spring, you’ll need to configure three ...

  6. 『重构--改善既有代码的设计』读书笔记----Move Field

    在类与类之间搬移状态和行为,是重构过程中必不可少的步骤.很有可能在你现在觉得正常的类,等你到了下个礼拜你就会觉得不合适.或者你在下个礼拜创建了一个新的类并且你需要讲现在类的部分字段和行为移动到这个新类 ...

  7. [ExtJS5学习笔记]第十九节 Extjs5中通过设置form.Panel的FieldSet集合属性控制多个field集合

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39209533 官方例子:http://docs.sencha.com/extjs/5. ...

  8. 《FDTD electromagnetic field using MATLAB》读书笔记之 Figure 1.14

    背景: 基于公式1.42(Ez分量).1.43(Hy分量)的1D FDTD实现. 计算电场和磁场分量,该分量由z方向的电流片Jz产生,Jz位于两个理想导体极板中间,两个极板平行且向y和z方向无限延伸. ...

  9. Lucene in action 笔记 term vector——针对特定field建立的词频向量空间,不存!不会!影响搜索,其作用是告诉我们搜索结果是“如何”匹配的,用以提供高亮、计算相似度,在VSM模型中评分计算

    摘自:http://makble.com/what-is-term-vector-in-lucene given a document, find all its terms and the posi ...

随机推荐

  1. 【转载】解决gridview空行时不显示的问题

    问题: GridView控件应用很是广泛,通常将它与DataSourceControl搭配使用,当然也可以手工指定DataSource属性来完成数据绑定.如果数据源返回一个空行的数据集(例如查询不到指 ...

  2. hdu 1045

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. (转载)Java Map中的Value值如何做到可以为任意类型的值

    转载地址:http://www.importnew.com/15556.html     如有侵权,请联系作者及时删除. 搬到我的博客来,有空细细品味,把玩. 本文由 ImportNew - shut ...

  4. python框架之Django(16)-接入Redis

    准备 安装Redis 参考 Ubuntu 中 Redis 的安装与使用. 在python中使用Redis 参考 python 中使用 Redis . 安装依赖包 在 Django 中接入 Redis ...

  5. Centos7下安装Docker(详细的新手装逼教程)

    早就听说过Docker,一直不清楚是个啥,今天捣鼓了一下,这里做个记录. --------------------------------------------------------------- ...

  6. outlook2016用Exchange轻松绑定腾讯企业邮箱

    系统版本:Win10 X64 1709 英文版 邮箱:Outlook2016 背景知识: 1.发送邮件均使用SMTP协议(SMTP 全称“Simple Mail Transfer Protocol”, ...

  7. [Python] Print input and output in table

    Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...

  8. Kaggle初学者五步入门指南,七大诀窍助你享受竞赛

    Kaggle 是一个流行的数据科学竞赛平台,已被谷歌收购,参阅<业界 | 谷歌云官方正式宣布收购数据科学社区 Kaggle>.作为一个竞赛平台,Kaggle 对于初学者来说可能有些难度.毕 ...

  9. shell脚本遍历当前目录下以数字命名的目录,并打印

    #!/bin/bash single='' #定义以个位数为目录的集合double='' #定位十位数为目录的集合#按照需要可以根据实际情况再定义以百位数为目录的集合 for dir in `ls - ...

  10. CentOS 7 keepalived+LVS

    LVS架构中 , 不管是NAT模式还是DR模式 , 当后端的RS宕机了 , 调度器还是会把请求转发到宕掉的RS上 , 然而keepalived可以解决该问题 , 它不仅仅有高可用的功能 , 还有负载均 ...