'''
连接数据库
databases = {
'default': {
'engine': 'django.db.backends.mysql'
'name':'数据库名称'
'user':''
'password':''
'host':''
'port':''
'charset':''
}
}
在任意__init__.py下写入
import pymysql
pymysql.install_as_MySQLdb()
models.py文件写法
from django.db import models
class 表明(models.Model):
a = models.xxx()
提交配置
python manage.py makemigrations
python manage.py migrate
ORM字段和参数
常用字段
IntegerField()
AutoField(primary_key=True)
CharField(max_length=32)
DateField()
DateTimeField()
auto_now=True
auto_now_add=True
DecimalField()
max_digit=
decimal_places
BooleanField()
EmailField()
TextField()
FileField()
常用参数
null=True
unique+=True
db_index=True
primary_key=True
default=''
verbose_name=''
auto_now
auto_now_add
max_length=32
choices=变量名
(
(1,),
(2,)
)
数据对象.get_choices_display()
blank=True
自定义字段
from django.db import models
class xxx(models.Model):
def __init__(self, max_length, *args, **kwargs):
self.max_length=max_length
super().__init__(max_length=max_length, *args,**kwargs)
def db_type(self, connection):
return 'chr(%s)'%self.max_length
关系字段
OneToOneField()
Man)yToManyField()
Foreignkey(
to=''
to_field=''
to_delete=models.
set(值/func)
set_default
set_null
cascade
do_nothing
protest
db_constraint=True
字段操作
增加
直接在类中增加即可,然后在终端写入以下代码激活
python manage.py makemigrations
python manage.py migrate
删除
修改
查看
'''