orm添加表记录
1.一对多的新增
方式一:
ret=Book.objects.create(name='红楼梦',price=34.5,publish_id=1)
方式二:
publish = Publish.objects.filter(pk=2).first()
ret = Book.objects.create(name='西游记', price=34.5, publish=publish)
2.一对多修改数据
方式一:
book=Book.objects.get(pk=1)
book.publish_id=2
book.save()
方式二:
book=Book.objects.filter(pk=1).update(publish=出版社对象)
book=Book.objects.filter(pk=1).update(publish_id=1)
3.一对多删除数据:同表删除
book=Book.objects.filter(pk=1).delete()
4.一对一和一对多相同
5.多对多
.add(添加数据)
book=Book.objects.filter(name='红楼梦').first()
book.authors.add(1,2)
.remove(删除)
book=Book.objects.filter(name='红楼梦').first()
book.authors.remove(2)
.clear(清空)
book=Book.objects.filter(name='红楼梦').first()
book.authors.clear()
.set(先清空,再新增,要传一个列表,列表内可以是id,也可以是对象)
book=Book.objects.filter(name='红楼梦').first()
book.authors.set([lqz,])