需求
了解models字段和参数
速查
models.py
1
2
3
|
class UserInfo(models.Model):
ctime = models.DateTimeField(auto_now = True ) #自动添加创建时间
uptime = models.DateTimeField(auto_now_add = True ) #自动添加更新时间
|
1
2
|
class UserInfo(models.Model):
img = models.ImageField(null = True ,blank = True, )
2
3
4
5
6
7
8
|
class UserInfo(models.Model):
name = models.CharField(max_length = 32 )
ctime = models.DateTimeField(auto_now = True ) #自动添加创建时间
uptime = models.DateTimeField(auto_now_add = True ) #自动添加更新时间
#Django特有方法,输出对象(models.table.object.all()获取的是一个对象)的某个值;
def __unicode__( self ):
return self .name
|
经过访问index页面,触发函数,生成一条数据
views.py
1
2
3
4
5
6
|
from app01 import models
def index(request):
models.UserInfo.objects. all ().delete() #清空数据
models.UserInfo.objects.create(name = 'user1' ) #创建user1
after_list = models.UserInfo.objects. all () #获取all()的对象
print after_list[ 0 ].ctime #打印第一条数据的创建时间
|
IDE
navicat
2、models.ImageField()
models.py
1
2
|
class UserInfo(models.Model):
img = models.ImageField(null = True ,blank = True )
|
browser
数据库
还可指定路径
1
2
3
|
class UserInfo(models.Model):
name = models.CharField(max_length = 32 )
img = models.ImageField(null = True ,blank = True ,upload_to = "upload" )
|