1. 程式人生 > >django之models學習總結

django之models學習總結

from django.db import models

# Create your models here.
class Classes(models.Model):
    '''
    班級表
    '''
    title=models.CharField(max_length=32)
    m=models.ManyToManyField('Teachers')

'''
obj=models.Classes.objects.filter(id=1).first()
obj.m.add(1)  代表建立班級id=1 老師Id=1資料物件
obj.m.add(2)  代表建立班級id=1 老師Id=2資料物件
 
obj=models.Classes.objects.filter(id=2).first()
obj.m.add(1)  代表建立班級id=2 老師Id=1資料物件
obj.m.add(2)  代表建立班級id=2 老師Id=2資料物件
obj.m.add([2,3])  代表建立班級id=2 老師id=2 id=3資料物件

'''
class Teachers(models.Model):
    '''
    老師表
    '''
    '''-----------單表-----------------
    每個資料物件在資料庫中會有一列自增的ID
    一、建立資料物件(增)
    建立資料物件的兩種方法
    1.models.Teachers.objects.create(name='xx')
    
    
    2.obj=Teachers(name='xx')
      obj.save()
      
      
      二、獲取資料物件(查)
      models.Teachers.objects.all() //獲取所有物件
      models.Teachers.objects.filter(id=1)//獲取某個物件
      models.Teachers.objects.filter(id=1,name='xx')
      models.Teachers.objects.filter(id__gt=1)//獲取ID大於等於1的物件
      models.Teachers.objects.filter(id__gt=1).first()//獲取ID大於等於1的物件中的第一個
      models.Teachers.objects.all()[:10] 切片操作,獲取10個人,不支援負索引,切片可以節約記憶體
      三、刪除資料物件(刪)
      models.objects.Teachers.filter(id=1).delete()//刪除ID=1物件
      
      四、修改資料物件
      models.Teachers.objects.all().update(name='xx')
      models.Teachers.objects.filter(id=1).update(name='xx')//有過濾條件的修改
    '''

    name=models.CharField(max_length=32)

class Student(models.Model):
    '''
    學生表
    '''

    username=models.CharField(max_length=32)
    age=models.IntegerField()
    gender=models.BooleanField()
    cs=models.ForeignKey(Classes,on_delete=models.CASCADE) #ForeignKey約束的物件在資料庫中會自帶下劃線__id,此處的cs 裡面包含id 和title


    '''
    學生物件在資料庫中右5列標識

    id     username        age      gender      cs__id
    
    
    1.建立資料物件(增)
    models.Students.objects.create(username='xx',age=20,gender='男',cs__id=1)
    models.Students.objects.create(username='xx',age=20,gender='男',cs=models.Class.objects.filter(id=1).first())
    班級:
    Id     title
    1     軟體工程
    2     嵌入式班
    3     大資料班
    Students類中的cs代表是班級裡的一行資料:如:1 軟體工程(即:id和title),若要取班級的中ID或title中的一項,
    需要採用cs__id或cs__title
    2.查詢資料(查)
    res=models.Students.objects.all() //獲取的是多條資料
    for item in  res:
        print(item.id)
        print(item.username)
        print(item.age)
        print(item.gender)
        print(item.cs__id)  
        print(item.cs.id)   #for迴圈跨表的時候可以使用cs__id或cs.id 
        print(item.cs__title)
        print(item.cs.title)
        
    3.刪除(刪)
    
    models.Students.objects.filter(id=1).delete()
    models.Students.objects.filter(cs__id=1).delete()
    models.Students.objects.filter(username='xxx').delete()
    
    
    cid=input('請輸入班級ID')
    models.Students.objects.filter(cs__id=cid).delete()
    ctitle=input('請輸入班級名稱')
    models.Students.objects.filter(cs__title=ctitle).delete()
    models.Students.objects.filter(cs.title=ctitle).delete() 
    #cs.title=ctitle不成立,for迴圈中可以使用cs__id或cs.id,但是此處只能使用cs__id
    
    
    4.修改資料(改) 
    
    models.Students.objects.filter(id=1).update(username='xxx')
    
    '''

#多對多
'''
班級:
Id   title
1    網路1班
2    軟體1班

老師:
Id      name
1       Lee
2       Mark
3       Amie

老師--班級關係對應表(類   隱藏的多對多資料表)
id    TeacherID    ClassId
1        1            2
1        1            2
2        2            1
2        2            1

'''

'''
總結:
1.models中的一個類代表數庫中的一個表,類中的一個物件代表資料表中的一行記錄
2.Fk欄位代表關聯表中的一行資料
3.manyTomany欄位,自動生成第三表,依賴關聯表對第三張表進行操作。
'''