Field笔记

一:时区的转换

1、navie 时间 和 aware 时间

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

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

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

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

二:orm_intro_demo项目:

models.py:

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

views.py:

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

三:外键 (orm_ForeignKey项目)

models.py:

 1 # 外键
 2 class Category(models.Model):
 3     name = models.CharField(max_length=100)
 4 
 5 class Article(models.Model):
 6     title = models.CharField(max_length=100)
 7     content = models.TextField()
 8     # 外键模型:class Foreign(to,on_delete,**options);
 9     # to:表引用的模型;
10     # on_delete:后加CASCAEDE、SER_NULL等方法表外键引用的模型数据被删时的处理方法;
11     # category = models.ForeignKey('Category',on_delete=models.CASCADE)
12     # SET_DEFAULT:引用的数据被删除时可调用默认的值
13     category = models.ForeignKey('Category',on_delete=models.SET_DEFAULT,null=True,default=Category.objects.get(pk=1))
14 # app.models_name :应用名.模型名
15 # 引用的模型不在同个App下时使用,同App下可直接用模型名或self表示;
16 # 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')

猜你喜欢

转载自www.cnblogs.com/liqiongming/p/10440265.html
今日推荐