1. 程式人生 > 其它 >2021/6/15 django ORM(未完)

2021/6/15 django ORM(未完)

'''
連線資料庫
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
刪除
修改
檢視

'''