In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 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.
Table structure design
Or from a practical point of view.
Suppose, now I need to design a simple library management system, which is the kind of books bought, not library books!
I thought about it, first of all, there must be a list of books, dedicated to storing information about books, at least.
But after thinking about it, it seems that if I want to publish my book, I must need a publishing house to help me publish it. I certainly can't publish it myself, so it's like this.
On second thought, the book must be written by people, there must be an author, so we also need an author table, it should be like this.
On second thought, if this book is compiled by ordinary people, it will be a bit difficult. It is generally done by many people, so there is probably such a table.
In fact, the above missed a thing, the book needs to belong to a publishing house, so, the final table structure should be like this!
Django models code
From django.db import models # author Table class Author (models.Model): name= models.CharField (verbose_name= "author name", max_length=8) age = models.IntegerField (verbose_name= "author Age") phone = models.CharField (verbose_name= "author contact Information", max_length=11) # Publishing class Publish (models.Model): title = models.CharField (verbose_name= "Press name" Max_length=16) phone = models.CharField (verbose_name= "Publishing contact Information", max_length=11) # Book class Book (models.Model): title = models.CharField (verbose_name= "title", max_length=32) price = models.DecimalField (verbose_name= "Price", max_digits=5, decimal_places=2) PublishDate = models.DateField (verbose_name= "first Edition date") publish = models.ForeignKey (to=Publish, verbose_name= "Publishing House" On_delete=models.CASCADE) # Many author class BookManyAuthor (models.Model): book = models.ForeignKey (to=Book, verbose_name= "belonging to the Book", on_delete=models.CASCADE) author = models.ForeignKey (to=Author, verbose_name= "belonging to the author", on_delete=models.CASCADE)
Mysql.sql
The data is consistent with the above Excel diagram!
Web_author.sql web_book.sql web_bookmanyauthor.sql web_publish.sql
Query operation
This time we use Django ORM alone, so there is no need to run Django, so there is no need to write url or anything!
Pre-import
Import os import django # django_orm_demo is my project name os.environ.setdefault ("DJANGO_SETTINGS_MODULE", "django_orm_demo.settings") django.setup () # Import models must be from web import models after django.setup ()
Query all (all)
Grammar
Models..objects.all ()
For example: query all authors
Author_list = models.Author.objects.all () print (author_list)
Execution result
For example: query all books and traverse the details
Book_list = models.Book.objects.all () for book in book_list: print (book.title, book.price, book.PublishDate, book.publish)
Execution result
Query specified criteria (filter)
Grammar
Models..objects.filter ()
For example: inquire about Zhang San's information
Author = models.Author.objects.filter (name= "Zhang San") print (author) print (author.name)
Execution result
Maybe you found a mistake, 'QuerySet' object has no attribute' name'.
This is because filter () query, may be more than one value, just like name= Zhang San, there may be a lot of Zhang San!
So what I get is a list, notice the first red circle, what if I know it's a value, it's like taking the first value?
First
Through first, you get the first value, which is also the model class object.
Code
Author = models.Author.objects.filter (name= "Zhang San"). First () print (author,type (author)) print (author.name,author.phone)
Execution result
Filter supports multiple conditions
Suppose there are two Zhang San
What if I want to take the second Zhang San?
It would be nice if I could write two conditions, such as name= Zhang San and age=22.
Note: filter can only be followed by filter or first,filter. The condition in the query is and.
Code
Multiple conditions in # filter are and query author = models.Author.objects.filter (name= "Zhang San", age=22). First () print (author,type (author)) print (author.name,author.phone)
Execution result
List of commonly used filter conditions
Anyone familiar with Mysql may know that there are =, 1 # > = field _ _ gte=1 # native SQL condition: where field > = 1 # < field _ _ lt=1 # native SQL condition: where field < 1 #
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.