1.一对一和一多对多是一样的,多对多不太一样

django 外键的增删改查,一对多和多对多_数据

 

 

2.增 post (一对多)

class BookView(APIView):


def post (self,request,*args,**kwargs):
publish=Publish.objects.filter(pk=1).first()
author=Author.objects.filter(pk=1)
Book.objects.create(book_name="红楼",publish=publish) # 自动关联出版社,publish_id ,publish 也可以
#方法2,新增数据
book_objects=Book(book_name="aa",publish=publish)
book_objects.save()
# 删除
Publish.objects.filter(pk=2).delete()
# 修改
Book.objects.filter(pk=1).update(publish_id=1)