[Django] get and filter are different in Django's orm, djangoorm
Get and filter are different in Django's orm.
The Django orm framework is good for applications with low business complexity. It is easy to write and use. For beginners, the two longest-used get and filter methods in the query operation sometimes make some minor errors when you don't pay attention to them. The two methods in this section are different today.
I often use django of version 1.5. Let's take it as an example.
Document
First, compare the explanations in the two function documents.
Get
Returns the object matching the given lookup parameters, which shoshould be in the format described in Field lookups.
Get () raises MultipleObjectsReturned if more than one object was found. The MultipleObjectsReturned exception is an attribute of the model class.
Get () raises a DoesNotExist exception if an object wasn' t found for the given parameters. This exception is also an attribute of the model class
Filter
Returns a new QuerySet containing objects that do not match the given lookup parameters.
The lookup parameters (** kwargs) shocould be in the format described in Field lookups below. multiple parameters are joined via AND in the underlying SQL statement, and the whole thing is enclosed in a NOT ().
Input parameters
GetParameters can only be those defined in the model, and only strict matching is supported.
FilterCan be a field or an extended where query keyword, such as in and like.
Return Value
GetThe returned value is a defined model object.
FilterThe returned value is a new QuerySet object, and then a new QuerySet object can be returned when QuerySet is being queried. The chain operation is supported.
QuerySet is a collection object, which can be iterated or traversed, sliced, and so on,Not equal to the list type (Be sure to use)
Exception
GetIt is normal only when a record is returned, which means that the get query field must be a primary key or a field with unique constraints. An exception is thrown when multiple records are returned or no records are found.
FilterAny matching record is acceptable.
This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/38597593
How does django directly use model code (ORM) to test and view the database query results on the spot?
Use python manage. py shell in the django Project
From django. db import connection
# Import-related model class
# Query execution
Print connection. queries
Common Django Syntax: Method for accepting get and post Parameters
Name = request. REQUEST. get ('name', 'xxx ')
# Get/post requests can use this to receive Parameters
# Obtain the value of the parameter name. If there is no name in the request, assign xxx to the name.