A misinformation about the distinct of the database Operations API in Django

Source: Internet
Author: User

A misinformation about the distinct of the database Operations API in Django

Recently in the project of doing a server, the background framework is Apache MOD_WSGI + Django.
Django is a Python-based web development framework that is very powerful and how powerful it is for readers to experience it themselves.
One of the things I'm going to say here is about the ORM functionality of Python.
The problem is in the distinct () function in the database operations API provided by Django, and the reader of the SQL statement should know that the DISTINCT keyword can be weighed in the select operation. The distinct () function in Django is also the function, the usual usage is that we want to take out a table of all the values of a column, and only remove the duplicates, if there are duplicates only to take out once, almost all the network on the use of the function of the Python code corresponding to the following:

1 Xxxx.objects.values ("Field_name"). Distinct ()

#或者

1 Xxxx.objects.distinct (). VALUES ("Field_name")

But this usage often does not reach the user's intentions, you can look at the network of a wide spread of a question and answer:
Like this: http://bbs.csdn.net/topics/330006477
Another example: http://www.douban.com/group/topic/17856879/
In fact, here is an example, look at this answer carefully

—–
It is important to note that this returns a list of dictionaries instead of the usual QuerySet

This is not filtered out, this is a problem, or there are 5 objects in it, the result using Len (obj) know that there is no change, but if only for the statistics of the number of non-repeating data, or accurate 3
Obj=classname.objects.values (' name '). DISTINCT ()
Len (obj) =5
Obj.count () =3
However, there is no filtering effect in the result set, this problem needs to be solved!!!!!

Well-reasoned, convincing that the distinct () function does not implement the functionality it describes. Is that really the case?

Here is responsible to tell the reader that the above explanation is wrong, distinct () has the function of the weight is not necessary to doubt, then why the above explanation? Because he doesn't know. The distinct () function has a hidden property, and when you use the distinct () function, if you do not follow with the order_by () function, the function automatically takes the default sort field in the current table as a column of distinct. So the problem arises because he does not use order_by (' name ') to mask the distinct () feature, so that the distinct result is not simply a column of name, but rather a id+ The value of the name two column as the distinct parameter, you can see that the values in the ID column are all unique values, so the result must be the value of the ID column is the standard, which is

1 Len (obj) =5

, and if you want to achieve the needs of the landlord in the above example, the correct code is:

1 ClassName.objects.values (' name '). Distinct (). order_by (' name ')

In the 1.4+ version of Django, the distinct () function can pass in a parameter, eliminating the need to add a order_by () function, but this also requires that your database supports PostgreSQL, otherwise it is invalid.

Attached, Django official introduction:

Note

Any fields used in An order_by ()  call is included in the Sql select  columns. This can sometimes leads to unexpected results if used in conjunction with distinct ( ). If you order by fields from a related model, those fields would be added to the selected columns and they could make Otherwis E duplicate rows appear to be distinct. Since the extra columns don ' t appear in the returned results (they is only there-support ordering), it sometimes looks Like NON-DISTINCT results is being returned.

Similarly, if you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model Orde Ring) 'll still is involved and may affect uniqueness of the results.

The moral is, if you, is using the being distinct() careful about ordering by related models. Similarly, when using distinct() and values() together, is careful when ordering by IS in the values() .

A misinformation about the distinct of the database Operations API in Django

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.