models.DateTimeField 日期类型 datetime
参数:
auto_now = True :则每次更新都会更新这个时间
auto_now_add 则只是第一次创建添加,之后的更新不再改变。
class UserInfo(models.Model):
2 name = models.CharField(max_length=32)
3 ctime = models.DateTimeField(auto_now=True)
4 uptime = models.DateTimeField(auto_now_add=True)
from app01 import models
2 def home(request):
3 models.UserInfo.objects.create(name='yangmv')
4 after = models.UserInfo.objects.all()
5 print after[0].ctime
6 return render(request, 'app01/home.html')
如果不需要在程序中特别处理时区(timezone-aware),在Django项目的settings.py文件中,可以直接设置为“USE_TZ = False”防止时间不对
models.ImageField 图片
models.GenericIPAddressField IP
ip = models.GenericIPAddressField(protocol="ipv4",null=True,blank=True)
img = models.ImageField(null=True,blank=True,upload_to="upload")
本文详细介绍了Django中日期时间字段的使用方法,包括auto_now和auto_now_add的区别,并展示了如何在模型中设置图片和IP地址字段。
287

被折叠的 条评论
为什么被折叠?



