一对多外键增删改查
作者:互联网
# 一对多外键增删改查 # 增 # 1 直接写实际字段 id # models.Book.objects.create(title='论语',price=899.23,publish_id=1) # models.Book.objects.create(title='聊斋',price=444.23,publish_id=2) # models.Book.objects.create(title='老子',price=333.66,publish_id=1) # 2 虚拟字段 对象 # publish_obj = models.Publish.objects.filter(pk=2).first() # models.Book.objects.create(title='红楼梦',price=666.23,publish=publish_obj) # 删 # models.Publish.objects.filter(pk=1).delete() # 级联删除 # 修改 # models.Book.objects.filter(pk=1).update(publish_id=2) # publish_obj = models.Publish.objects.filter(pk=1).first() # models.Book.objects.filter(pk=1).update(publish=publish_obj)
标签:Book,多外键,models,改查,publish,objects,增删,pk,id 来源: https://www.cnblogs.com/dzs894330350/p/16098301.html