Acquisition and Use of Management Objects in ORM Operation

When we use ORM to operate databases with foreign keys and many-to-many relationships, we inevitably use managed objects, but the acquisition of managed objects is easy to confuse, so let's record it.

Here are the models we're going to use

class Publisher(models.Model):
    pid = models.AutoField(primary_key=True)   #Primary key
    name = models.CharField(max_length=32,unique=True) #Name of Publishing House
    def __str__(self):
        return self.name

class Book(models.Model):
    id = models.AutoField(primary_key=True)   #Primary key
    title = models.CharField(max_length=32,unique=True)    #Title of the book
    publisher = models.ForeignKey("Publisher",on_delete=models.CASCADE,related_name="books")  #Foreign key

    def __str__(self):
        return "object:{}-{}>".format(self.title,self.id)

class Author(models.Model):
    id = models.AutoField(primary_key=True) #id of primary key author
    name = models.CharField(max_length=32,unique=True)   #Author's Name
    books = models.ManyToManyField(Book,)  #It means that the relationship between the author and the book is many to many.

1. Acquisition method

1. Based on foreign keys

We can see that there are foreign keys in the book table. The book table and the publishing house table are connected by foreign keys. We can only get the management object of the publishing house.
Acquisition method:
pub_obj = models.Publisher.objects.get(pid=1)
obj = pub_obj.books # Set related_name
 obj = pub_obj.books_set # Sets related_name

2. Based on many-to-many relationship

There are many-to-many relationships between Book and Author tables. Both Book class objects and Author class objects can obtain management objects.
We define many-to-many fields in the Author table

Author class object acquisition management object:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books # directly by setting the names of many-to-many fields

Book class object acquisition management object:
bk_obj = models.Book.objects.get(id=19)
obj = bk_obj.author # Sets related_name
 obj = bk_obj.author_set # does not set related_name

2. use

Common methods of managing objects:
all   #Get all the associated objects
set   #Setting Associated Objects
add   #Adding Associated Objects
remove  #Remove associated objects (single)
clear   #Clear the associated objects (all)
create  #Creating Associated Objects
all:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books
print(obj.all())

#We can get all the book objects associated with the author object with id 2
set:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books
obj.set(21)

#Books with id 2 as the author object are all set to books with id 21
#Note that the set id must exist in the book table
add:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books
obj.add(21)
obj.add(models.Book.objects.get(id=21))

#Adding a Book object with id 21 to the author object with id 2
#Note that the new id must be in the book table
remove:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books
obj.remove(21)

#Remove the author object with id 2 from the book object with id 21
#Note that the removed id itself is already associated
clear:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books
obj.clear()

#Clear all books associated with author objects with id 2
create:
ah_obj = models.Author.objects.get(id=2)
obj = ah_obj.books
obj.create(title="Robinson Crusoe",id=30)

#Create a id bit 30 for the author of id 2, named Robinson Crusoe.

summary
1. In classes where foreign keys exist, the exclusive rights of classes that write foreign keys cannot be obtained from managed objects.
2. In classes with many-to-many relationships, both sides can obtain management objects in different ways.

Added by gdogfunk on Fri, 04 Oct 2019 01:29:15 +0300