example:
class Person(model.Models)
name = model.CharField(max_length=30)
age = model.IntegerField()
class Book(model.Model)
title = model.CharField(max_length=30)
author = model.ManyToManyField(Person)
per1 = Person('Jack',10)
per1.save
per2 = Person('Rose',20)
per2.save
book1 = Book()
book1.title = 'booktitle'
book1.author.add(per1)
book1.author.add(per2)
现在我想像这样获得"book1"对象
{
'title':'booktitle',
'author':[
{
'name':'Jack',
'age':10,
}
]
}
"书1"中没有一位作者如何过滤相关字段?有人可以帮助我吗?
您可以使用:
Book.objects.filter(author="Jack").all()
您还可以筛选多个作者:
Book.objects.filter(author=["Jack", "Rose"]).all()