Django实战之models
入门使用Django默认使用的数据库——sqlite3
一、创建表
models.py文件中创建类
class info(models.Model):
# CharField类型不能为空,最少要指定一个长度
name = models.CharField(max_length=32) #
email = models.EmailField(max_length=32)
user_type = models.ForeignKey('UserType')
字段类型:
# 自动生成增列 = int(11)
models.AutoField
# 不要随便显示的自定义一个自增列
id = models.AutoField(primary_key=True)
# 字符串字段 必须 max_length 参数
models.CharField
# 布尔类型=tinyint(1) 不能为空,Blank=True
models.BooleanField
models.ComaSeparatedIntegerField 用逗号分割的数字=varchar
继承CharField,所以必须 max_lenght 参数
# 5、日期类型 示例2018-08-30
models.DateField
# 参数说明:auto_now,默认值为false,设置为true时,每次执行 save 操作时,将其值设置为当前时间,并且每次修改model,都会自动更新。
6、models.DateTimeField 日期类型 datetime
同DateField的参数
7、models.Decimal 十进制小数类型 = decimal
必须指定整数位max_digits和小数位decimal_places
8、models.EmailField 字符串类型(正则表达式邮箱) =varchar
对字符串进行正则表达式
9、models.FloatField 浮点类型 = double
10、models.IntegerField 整形
11、models.BigIntegerField 长整形
integer_field_ranges = {
'SmallIntegerField': (-32768, 32767),
'IntegerField': (-2147483648, 2147483647),
'BigIntegerField': (-9223372036854775808, 9223372036854775807),
'PositiveSmallIntegerField': (0, 32767),
'PositiveIntegerField': (0, 2147483647),
}
12、models.IPAddressField 字符串类型(ip4正则表达式)
13、models.GenericIPAddressField 字符串类型(ip4和ip6是可选的)
参数protocol可以是:both、ipv4、ipv6
验证时,会根据设置报错
14、models.NullBooleanField 允许为空的布尔类型
15、models.PositiveIntegerFiel 正Integer
16、models.PositiveSmallIntegerField 正smallInteger
17、models.SlugField 减号、下划线、字母、数字
18、models.SmallIntegerField 数字
数据库中的字段有:tinyint、smallint、int、bigint
19、models.TextField 字符串=longtext
20、models.TimeField 时间 HH:MM[:ss[.uuuuuu]]
21、models.URLField 字符串,地址正则表达式
22、models.BinaryField 二进制
23、models.ImageField 图片
24、models.FilePathField 文件
更多字段
字段选项
null: True:允许None; 默认:False
blank: True:允许空白; 默认:False
db_column: 字段名; 默认:属性名
db_index: True:创建索引; 默认:False
default: 默认值
primary_key: True:主键
unique: True:值唯一
运行Djando项目程序,执行命令创建数据:
python3 manage.py makemigrations
python3 manage.py migrate
二、删除重建表
数据库中删除
drop table tablename
注释django中对应的Model
python manage.py makemigrations
python manage.py migrate --fake
去掉注释重新迁移
python manage.py makemigrations
python manage.py migrate
三、views文件引用数据库
def index(request):
info= models.info.objects.all() # pk:primary key(id),相当于select id =1
return render(request, 'index.html', {'info': info})
# 传递多个表格 info,test
def index(request):
info= models.info.objects.all() # pk:primary key(id),相当于select id =1
test = ["HTML", "CSS", "jQuery", "Python", "Django"]
return render(request, 'index.html', {'info': info, 'test':test})
如果key和value一定要不一样,html中引用时写key
比如’test1’:test
html中引用{{ test1 }}
三、html文件引用数据库
{% for info in info%}
<a href="">{{ info.name }}</a>
{% endfor %}
注意有时候html引用数据库怎么都显示不出来,可能是views和html文件中变量名没整对
干脆就把info的名字全部都换一边,比如test,发现可以引用了
再换回来也可以
0、重构数据库
误删了migrations文件,python manage.py makemigrations一直提示No changes detected
重构应用下的django数据库
先把migrations文件夹里文件全部删除,然后执行以下命令
# 先生成一个空的initial.py
python manage.py makemigrations --empty yourappname
python manage.py makemigrations;
python manage.py migrate
问题记录:
1、makemigrations时告警:
(urls.W005) URL namespace ‘admin’ isn’t unique
原因:运行命令时显示文件路径,E:\frontpage\quantmonitor\monitor/static
之前配置过静态文件聚合路径STATIC_ROOT
估计在静态文件聚合路径里找不到db.sqlite3文件
settings文件里注销STATIC_ROOT后运行makemigrations
提示:静态文件访问,接收,解析地址后,把相对路径传给static_root
attributeerror: module ‘settings’ has no attribute ‘STATIC_ROOT’
settings文件里恢复STATIC_ROOT后运行makemigrations正常
估计是环境没有刷新,比较莫名其妙
2、no migrations to apply
原因:之前已经成功在数据库中建好表,后面数据库直接删掉了
解决办法:
删除该app名字下的migrations文件
进入数据库,找到django_migrations的表,删除该app名字(monitor)的所有记录。
delete from django_migrations;
再次迁移
3、提示table “django_session” already exists
migrations后django_content_type里面没有要新增的表,是没法进行关联的。
django_content_type:记录了当前的Django项目中所有model所属的app(即app_label属性)以及model的名字(即model属性)
不能直接删
4、删除django_content_type表中数据时提示
Sqlite: FOREIGN KEY constraint failed
sqlite外键约束中。restrict约束:如果要删除父表,则子表需空。
如果没有定义约束。会报错,需设置一个约束。
在django_admin_log表里有一列content_type_id,关联django_content_type的id
5 、默认idl列丢失 no such column
django模型models中默认id问题 no such column
349



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



