[Django] use of f object in Django-ORM

Source: Internet
Author: User
Tags prefetch
Introduction to the use of f objects in Django-ORM

In the Django document, there is a section "query-related classes", which refers to join queries. 1.7 is newly added. The association here is the association of fields, rather than the association between tables.

Table join mainly uses three objects F (), Q (), and prefetch (), where prefetch is newly added by 1.7, and the other two are available in previous versions. I used to compare the two time fields in a table. I used the F object. Today I saw some new usage when I looked at the djangocon PPT. So I searched the document, summary.

Concept

Class F
F () indicates the value of the model field. That is to say, for some special field operations, we do not need to use python to get the data to the memory first, and then operate, it is stored in the DB.

Scenario

Several common scenarios

Field + 1 (addition, subtraction, multiplication, division)

For example, we have a field that counts clicks. Each update operation actually adds the field value to 1.

Generally, we take this record, add the corresponding field to + 1, and then save it, similar to the following code:

# Tintin filed a news story!reporter = Reporters.objects.get(name=‘Tintin‘)reporter.stories_filed += 1reporter.save()

What if we use F? Only one line of code is required

Reporters.objects.filter(name=‘Tintin‘).update(stories_filed=F(‘stories_filed‘) + 1)

This not only reduces the amount of code, but also increases the efficiency of operations directly in the data, especially in the case of concurrency, reducing the hidden danger of simultaneous operations by multithreading. However, string addition is not supported.

Field comparison

For example, a contract has two dates, one is the termination date and the other is the end date. Now, you need to filter out contracts whose termination date is earlier than the end date.

from django.db.models import Ffrom contracts.models import Contractscontracts = Contracts.objects.filter(contract_stop_time__lt=F(‘end_time‘))

If there is no f object, Rom cannot be used directly for query.

Summary

These two types of usage are found. If there are new or extended usage, they are being updated.

This article from the "orangleliu notebook" blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/40431839

[Django] use of f object in Django-ORM

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.