Old Wang Python: The difference between get and filter methods of Django Model

Source: Internet
Author: User

Old Wang Python: The difference between get and filter methods of Django model _ Python tutorial _ Old Wang Python

Old Wang Python: The difference between get and filter methods of Django model Django's get and filter methods are commonly used by Django model. It is very important to clarify the differences between them.

To illustrate the differences between the two, define two models.

Class student (models. Model ):
Name = models. charfield ('name', max_length = 20, default = '')
Age = models. charfield ('age', max_length = 20, default = '')

Class book (models. Model ):
Student = models. foreignkey (student)

1. Let's talk about Django's get method first:

1django's get method is to get a matching result from the database and return an object. If the record does not exist, it will report an error.
For example, if the name value of a record in my database is Old Wang python, student = student. Objects. Get (name = 'old Wang python') will be used '),
A record object is returned. You can use student. _ dict _. It returns a dictionary form, {'key': valeus}, key is the field name, and values is the value content.
If I use the get method to query records that do not exist in a database,ProgramAn error is reported.
For example, student = student. Objects. Get (name = 'old Wang '). You can run it on your own.

2. If you use Django get to obtain the data of the associated table, if there are more than two data records in the key table, an error is returned.
For example, my student table has a record:
ID name age
1 Python 24

Book table:

Id student_id
1 1
2 1

I use
Student = student. Objects. Get (name = 'python ')
Book = book. Objects. Get (student)
It also reports an error because the book table has two records that match the student table.

Ii. Let's talk about Django filter:

1django's filter method gets matching results from the database and returns an object list. If the record does not exist, it returns [].
For example, if the name value of a record in my database is Lao Wang python, student = student. Objects. Filter (name = 'lao Wang Python ') is used ')
The returned student is a list of objects. It can be seen that student [0] has the same result as the student returned by the get method above.

2. If you use Django get to retrieve data from the associated table, no error will be reported regardless of the number of records in the associated table.
In addition to the powerful model, Django also has powerful forms and templates. For details about the forms, see:
Django forms datetimefield
Django form display
Django Form Verification
Django Template

In addition, I can see from other materials that the filter seems to have the function of caching data. The first time I query the database and generate the cache, the next time I call the filter method, I will directly retrieve the cached data, the get method is used to directly query the database each time it is executed. I don't know if this is correct.

This is a bit of Django experience I have used for a while. I hope it will help you understand Django get and filter!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.