Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Django ORM operation

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand Django ORM operation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Django ORM operation.

Query operation

Object. Foreign key field

For example, we get the information about a book, and we can print his information like this.

Code

Book = models.Book.objects.filter (title= "). First () print (f" book Type: {type (book)} ") print (f" id: {book.id} ") print (f" Book title: {book.title} ") print (f" Price: {book.price} ") print (f" Book title: {book.PublishDate} ") print (f" Publishing House: {book.publish} ") # Foreign key field

Execution result

Note: blue is the foreign key field

I don't know if you have any questions about why book.publish printed it out in the Post and Telecommunications Press.

This is mainly due to the _ _ str__ method of the foreign key object.

It is because my Publish returns self.title, so I can print it out to the post and telecommunications publishing house. What if I want to print the contact information of the publishing house?

Code

Print (f "Publishing House Type: {type (book.publish)}") # # book.publish is already a models.Publish object, so you can freely adjust the attribute print (f "Publishing House phone: {book.publish.phone},")

Execution result

Summary

Object. The foreign key field gets the foreign key field object, which can be passed directly through the object. Foreign key field. The foreign key property gets the specific value.

Reverse query (table name _ _ set.all ())

Above, we got the specific information of the publishing house corresponding to the book by the way of forward query.

But what if what we get is the name of a publisher?

Usually, you might do this!

Code

# query posts and Telecommunications Publishing House publish_obj = models.Publish.objects.filter (title= "Post and Telecommunications Publishing House"). First () # get publisher id publish_id=publish_ obj.id # query book_list = models.Book.objects.filter (publish_id=publish_id) print (book_list) where publish_id is the publisher id

Execution result

In fact, there is another way: check multiple objects in reverse through one object.

Code

Publish_obj = models.Publish.objects.filter (title= Post and Telecommunications Press). First () book_list = publish_obj.book_set.all () print (book_list)

Execution result

Double underscore cross-table query

Or the above question, through the name of a publishing house, to find the books belonging to this publishing house.

Cross-table query based on double underscore, the theory is simpler!

Note: you can see that there are other filter conditional queries such as _ _ contains, which can still be used through _ _ cross-table.

Code

Book_list = models.Book.objects.filter (publish__title= Post and Telecommunications Press) print (book_list)

Execution result

Continuous cross-table

_ _ can span not only one table, but also multiple tables.

Take the book Many author table as an example, query the many-to-many information of books and authors according to the publishing house.

Code

Ret = models.BookManyAuthor.objects.filter (book__publish__title= Post and Telecommunications Press) print (ret)

Cross book table and cross publish table

Execution result

Values

Sometimes, we may only need some specific columns, and then we can just use values.

Code

# Syntax book_list = models.Book.objects.all (). Values ("column 1", "column 2",...) # example book_list = models.Book.objects.all () .values ("title", "price") print (book_list)

Code

The value returned by values is a bit like a list set dictionary, but it is essentially a QuerySet type.

Values_list

Values_list has the same function as values, which takes related columns, but returns different types in format.

Code

Book_list = models.Book.objects.all () .values_list ("title", "price") print (book_list)

Execution result

This is a bit like a list tuple, but it is essentially QuerySet.

Related_name

Related_name is usually used to replace _ set in reverse queries.

Original mode

Models.py

Code

# query posts and Telecommunications Press publish = models.Publish.objects.filter (title= Post and Telecommunications Press). First () print (publish) # reverse one-to-many book_list = publish.book_set.all () print (book_list)

Execution result

Alias mode

Models.py

Code

# query posts and Telecommunications Press publish = models.Publish.objects.filter (title= Post and Telecommunications Press). First () print (publish) # reverse one-to-many book_list = publish.book_list.all () print (book_list)

Execution result

Filter (). Filter ().

As we said above, it supports multiple filter, filter (). Filter ().

This situation is usually used in the case of uncertain filter criteria, but in the case of multi-layer filtering.

Code

# for example, the following filter can have other conditions book1 = models.Book.objects.filter (title= "") .filter (price= "99") # the effect is the same as above book2 = models.Book.objects.filter (title= ", price=" 99 ") print (book1) print (book2)

Execution result

At this point, I believe you have a deeper understanding of "how to understand Django ORM operation", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report