[Django] Get and filter differences in Django's ORM

Source: Internet
Author: User
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

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.