四、Django学習の関係表の紹介と使用

4276 ワード

リレーショナル・テーブルの紹介と使用
一対一の関係
xx = models.OneToOneField(to='  ',to_field='   ',on_delete=models.CASCADE)  
#on_delete:          ,to_field    ,             ,on_delete 1.x   django    ,        ,2.x   django  .

データの追加
方式1
new_author_detail = models.AuthorDetail.objects.create(
        birthday='1979',
        telephone='138',
        addr='black'
    )
    #   1
    models.Author.objects.create(
        name='  ',
        age=18,
        #         
        authorDetail=new_author_detail 
    )

方式2
new_author_detail = models.AuthorDetail.objects.create(
        birthday='1979',
        telephone='138',
        addr='black'
    )
    #   2
    models.Author.objects.create(
        name='  ',
        age=18,
        authorDetail_id=new_author_detail.id
    )

データの削除
表1の外部キーは表2に関連付けられ、表1は削除され、表2に影響しません.表2の削除は表1に影響します.
models.AuthorDetail.objects.get(id=2).delete()
models.Author.objects.get(id=3).delete()

データの更新
models.Author.objects.filter(id=5).update(
        name='   ',
        age=16,
        authorDetail=models.AuthorDetail.objects.get(id=5),
        authorDetail_id=4,
     )

一対多の関係
xx = models.ForeignKey(to='  ',to_field='   ',on_delete=models.CASCADE)

データの追加
方式1
外部キー関連の直接割り当ては、オブジェクトにすればよい
obj = models.Publish.objects.get(id=1)
models.Book.objects.create(
    title='  ',
    publishDate='2019-07-22',
    price=3,
    publishs=obj
)

方式2
obj = models.Publish.objects.get(id=1)
models.Book.objects.create(
        title='  test',
        publishDate='2019-07-22',
        price=3.5,
        publishs_id=1 #     ,         (id)
        #publishs_id=obj.id
    )

データの削除
models.Publish.objects.get(id=1).delete()
models.Book.objects.get(nid=1).delete()

データの更新
models.Book.objects.filter(pk=4).update(
title='B    2',
publishs=models.Publish.objects.get(id=3),
publishs_id=3, #           
    )

多対多関係
xx = models.ManyToManyField(to='      ') #         

データの追加
book_obj = models.Book.objects.get(nid=1)
    book_obj.authors.add(*[1,2])

データの削除
book_obj = models.Book.objects.get(nid=6)
book_obj.authors.remove(6)#                   。
book_obj.authors.remove(*[5,6])
book_obj.authors.clear() #   nid 6      
book_obj.authors.add(*[1,])#     
book_obj.authors.set('1')#        
book_obj.authors.set(['5','6']) #      

from django.db import models

# Create your models here.

from django.db import models

# Create your models here.
#   
class Author(models.Model): #              
    name=models.CharField( max_length=32)
    age=models.IntegerField()
    # authorDetail=models.OneToOneField(to="AuthorDetail",to_field="nid",on_delete=models.CASCADE)
    authorDetail=models.OneToOneField(to='AuthorDetail')  #    AuthorDetail           ,     authorDetail_id       

    #      --      _id

    # foreign+unique

    def __str__(self):
        return self.name

#       
class AuthorDetail(models.Model):
    birthday=models.DateField()
    # telephone=models.BigIntegerField()
    telephone=models.CharField(max_length=32)
    addr=models.CharField( max_length=64)
    def __str__(self):
        return self.addr


#                     
class Publish(models.Model):
    name=models.CharField( max_length=32)
    city=models.CharField( max_length=32)
    email=models.EmailField()  #charfield -- asdfasdf
    def __str__(self):
        return self.name

#   
class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField( max_length=32)
    publishDate=models.DateField()
    price=models.DecimalField(max_digits=5,decimal_places=2)  #decimal(16,2)
    publishs=models.ForeignKey(to="Publish")
    authors=models.ManyToManyField(to='Author',)

    def __str__(self):
        return self.title