json pass the object dictionary

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': ' red', 'Age': 22 is}
{ 'ID':. 1, 'name ':' red ',' 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': 'red', 'id': 1, 'age': 22, 'book__title': 'Alice and Bob's book', 'book__price': 11}, { 'name': 'Small red ',' id ': 1,' age ': 22,' book__title ':' and Li red book ',' book__price ': 22}, {' name ':' red ',' id ': 1, 'age': 22, 'book__title': 'own red book', 'book__price': 66}]

    authors = Author.objects.filter(book__title__icontains="书").values("name","id","age","book__title","book__price")
    print(list(authors))

  

[{ 'Name': 'red', 'id': 1, 'age': 22, 'book__title': 'Alice and Bob's book', 'book__price': 11}, { 'name': 'Bob ',' id ': 2,' age ': 24,' book__title ':' Alice and Bob's book ',' book__price ': 11}, {' name ':' red ',' id ': 1, 'age': 22, 'book__title': 'and Li red book', 'book__price': 22}, { 'name': 'Li', 'id': 3, 'age': 33, ' book__title ':' and Li red book ',' book__price ': 22}, {' name ':' Bob ',' id ': 2,' age ': 24,' book__title ':' and Li Xiaoming the book ',' book__price ': 33}, {' name ':' Li ',' id ': 3,' age ': 33,' book__title ':' and Li Xiaoming book ',' book__price ': 33}, { 'name': 'red', 'id ': 1,' age ': 22,' book__title ':' own red book ',' book__price ': 66}, {' name ':' Li ',' id ': 3,' age ': 33, 'book__title': 'Li his book', 'book__price': 45}]

 

    books = Book.objects.filter(authors__name__icontains="小红").values("authors__age","authors__name","title")
    print(list(books))

  

[{ 'Authors__age': 22, 'authors__name': 'red', 'title': 'Alice and Bob's book'}, { 'authors__age': 22, 'authors__name': 'red', 'title' : 'red book and Li'}, { 'authors__age': 22, 'authors__name': 'red', 'title': 'own red book'}]

 

Get object class table by the presence book.author relation table field

# Add: add author's primary key or object to their books
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


# Delete: Delete the existing books of their primary key or object
book.author.remove(*authors)
Delete key or by primary target
b1.author.remove(a1)
b1.author.remove(a2.id)
All Clear
b1.author.clear()


# Change: Empty and add the primary key or object of 
book.author.clear()
book.author.add(*authors)
Primary key or set of objects in the form of a list
Remove the value does not exist in the new data, add new data and some new values, before repeating values ​​are reserved (value before repeating the same information)
book.author.set ([* author]) # value must be passed in the form of a list

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}

 

 

Guess you like

Origin www.cnblogs.com/realadmin/p/12014363.html