json传递对象字典
authors = Author.objects.filter(name="小非").first()
if authors:
print(authors.__dict__)
info_dic = authors.__dict__
info_dic.pop("_state")
a = json.dumps(info_dic,ensure_ascii=False)
a = json.loads(a)
print(a)
else:
print("没有找到匹配项")
{'_state': <django.db.models.base.ModelState object at 0x105466b70>, 'id': 1, 'name': '小红', 'age': 22}
{'id': 1, 'name': '小红', 'age': 22}
model: class Author(models.Model): name = models.CharField(max_length=30) age = models.IntegerField()] class Book(models.Model): title = models.CharField(max_length=100) price = models.IntegerField() authors = models.ManyToManyField(Author)
authors = Author.objects.filter(name="小红").values("name","id","age","book__title","book__price") print(list(authors))
[{'name': '小红', 'id': 1, 'age': 22, 'book__title': '小红和小明的书', 'book__price': 11}, {'name': '小红', 'id': 1, 'age': 22, 'book__title': '小红和小李的书', 'book__price': 22}, {'name': '小红', 'id': 1, 'age': 22, 'book__title': '小红自己的书', 'book__price': 66}]
authors = Author.objects.filter(book__title__icontains="书").values("name","id","age","book__title","book__price") print(list(authors))
[{'name': '小红', 'id': 1, 'age': 22, 'book__title': '小红和小明的书', 'book__price': 11}, {'name': '小明', 'id': 2, 'age': 24, 'book__title': '小红和小明的书', 'book__price': 11}, {'name': '小红', 'id': 1, 'age': 22, 'book__title': '小红和小李的书', 'book__price': 22}, {'name': '小李', 'id': 3, 'age': 33, 'book__title': '小红和小李的书', 'book__price': 22}, {'name': '小明', 'id': 2, 'age': 24, 'book__title': '小明和小李的书', 'book__price': 33}, {'name': '小李', 'id': 3, 'age': 33, 'book__title': '小明和小李的书', 'book__price': 33}, {'name': '小红', 'id': 1, 'age': 22, 'book__title': '小红自己的书', 'book__price': 66}, {'name': '小李', 'id': 3, 'age': 33, 'book__title': '小李自己的书', 'book__price': 45}]
books = Book.objects.filter(authors__name__icontains="小红").values("authors__age","authors__name","title") print(list(books))
[{'authors__age': 22, 'authors__name': '小红', 'title': '小红和小明的书'}, {'authors__age': 22, 'authors__name': '小红', 'title': '小红和小李的书'}, {'authors__age': 22, 'authors__name': '小红', 'title': '小红自己的书'}]
通过关系表字段存在的类的对象获取关系表 book.author # 增:为书籍添加作者的主键或对象们 book.author.add(*authors) a1 = Author.objects.first() a2 = Author.objects.all()[1] b1 = Book.objects.first() b2 = Book.objects.all()[1] b1.author.add(a1.id, a2.id) # type:Book b2.author.add(a1) # type:Book # 删:删除书籍已有作者的主键或对象们 book.author.remove(*authors) 通过主键或者对象删除 b1.author.remove(a1) b1.author.remove(a2.id) 全部清空 b1.author.clear() # 改:清空并添加作者的主键或对象 book.author.clear() book.author.add(*authors) 设置作者的主键或对象形式的列表 去除新数据中不存在的值,添加新数据中新有的值,与之前重复的值保留(之前重复的值信息不变) book.author.set([*author]) # 传入值必须为列表形式 b2.author.set([a1,a2]) b2.author.set([a1.id])
books = Book.objects.filter(authors__name__icontains="小红").first() author = books.authors.all() for i in author: print(i.__dict__)
{'_state': <django.db.models.base.ModelState object at 0x10550c198>, 'id': 1, 'name': '小红', 'age': 22}
{'_state': <django.db.models.base.ModelState object at 0x10550c208>, 'id': 2, 'name': '小明', 'age': 24}