{"id":457999,"date":"2025-04-29T09:32:36","date_gmt":"2025-04-29T09:32:36","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=457999"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=457999","title":{"rendered":"<span>F(), Func() \u0438 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0446\u0438\u043a\u043b\u043e\u0432: \u043a\u0430\u043a Django \u0434\u0443\u043c\u0430\u0435\u0442 \u0432 SQL<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/strong><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>F()<\/code>\u2011\u044d\u043a\u0441\u043f\u0440\u0435\u0441\u0441\u0438\u0438 \u0438 <code>Func()<\/code>\u2011\u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0432\u00a0Django \u0434\u043b\u044f\u00a0\u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0443, \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong>\u043d\u0435\u00a0\u0432\u00a0Python, \u0430\u00a0\u043d\u0430\u00a0\u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/strong>. \u042d\u0442\u043e \u0432\u0430\u0436\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0440\u0435\u0447\u044c \u0438\u0434\u0451\u0442 \u043e:<\/p>\n<ul>\n<li>\n<p>\u043c\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\u0445 \u0431\u0435\u0437 <code>save()<\/code>,<\/p>\n<\/li>\n<li>\n<p>\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u0438,<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u00a0\u0432\u044b\u0441\u043e\u043a\u0438\u0445 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430\u0445,<\/p>\n<\/li>\n<li>\n<p>\u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u00a0\u2014 \u0443\u043c\u0435\u043d\u0438\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u0432\u00a0\u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 SQL, \u043d\u0435\u00a0\u0442\u0435\u0440\u044f\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 Django ORM.<\/p>\n<\/li>\n<\/ul>\n<h3>F(): \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u043e\u043b\u044f<\/h3>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u00a0\u2014 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u0435 <code>views<\/code>:<\/p>\n<pre><code class=\"python\">article = Article.objects.get(id=1) article.views += 1 article.save()<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043c\u0435\u0436\u0434\u0443 <code>get()<\/code> \u0438 <code>save()<\/code> \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0433\u043e\u043d\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u00a0\u2014 \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u043e\u0432 \u043f\u043e\u0442\u0435\u0440\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<pre><code class=\"python\">from django.db.models import F  Article.objects.filter(id=1).update(views=F('views') + 1)<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u00a0SQL \u0432\u0438\u0434\u0430 <code>UPDATE article SET views = views + 1 WHERE id = 1<\/code>. \u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u0433\u043e\u043d\u043a\u0438. \u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430\u00a0\u043f\u0430\u043c\u044f\u0442\u044c. <\/p>\n<h3>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044f\u043c\u0438<\/h3>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0442\u043e\u0432\u0430\u0440\u044b, \u0443\u00a0\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0446\u0435\u043d\u0430 \u0432\u044b\u0448\u0435 \u0441\u043a\u0438\u0434\u043a\u0438:<\/p>\n<pre><code class=\"python\">Product.objects.filter(price__gt=F('discount_price'))<\/code><\/pre>\n<p>\u0411\u0430\u0437\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u044f\u043c\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f. \u041d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u00a0Python, \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u00a0\u043b\u044e\u0431\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438: \u0447\u0438\u0441\u043b\u0430, \u0434\u0430\u0442\u044b, \u0431\u0443\u043b\u0435\u0432\u044b, \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<h3>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u043e\u043b\u044f \u0447\u0435\u0440\u0435\u0437 annotate()<\/h3>\n<p>\u041f\u0443\u0441\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u0432\u00a0\u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043d\u0430\u00a0\u0441\u043a\u043b\u0430\u0434\u0435:<\/p>\n<pre><code class=\"python\">from django.db.models import F, ExpressionWrapper, DecimalField  Product.objects.annotate(     stock_value=ExpressionWrapper(         F('price') * F('stock'),         output_field=DecimalField()     ) )<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 DRF, <code>stock_value<\/code> \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0435 \u043a\u0430\u043a\u00a0\u043f\u043e\u043b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438\u00a0\u2014 \u0431\u0435\u0437\u00a0\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<h3>\u0423\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f: Case \/ When<\/h3>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0435\u0441\u043b\u0438 <code>stock == 0<\/code>, \u0445\u043e\u0442\u0438\u043c \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 <code>\"\u041d\u0435\u0442 \u0432 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\"<\/code>, \u0438\u043d\u0430\u0447\u0435 <code>\"\u0412 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\"<\/code>:<\/p>\n<pre><code class=\"python\">from django.db.models import Case, When, Value, CharField  Product.objects.annotate(     availability=Case(         When(stock=0, then=Value(\"\u041d\u0435\u0442 \u0432 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\")),         default=Value(\"\u0412 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\"),         output_field=CharField()     ) )<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441\u2011\u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u044f\u043c\u043e \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440, \u0431\u0435\u0437\u00a0\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u00a0Python.<\/p>\n<h3>\u041c\u0430\u0441\u0441\u043e\u0432\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0412\u00a0\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0435\u0439\u0441: \u0435\u0441\u043b\u0438 <code>progress_percent &gt;= 100<\/code>, \u0442\u043e <code>status = 'completed'<\/code>.<\/p>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u0443\u0447\u0448\u0435 \u0437\u0430\u0431\u044b\u0442\u044c:<\/p>\n<pre><code class=\"python\">for task in Task.objects.all():     if task.progress_percent &gt;= 100:         task.status = 'completed'         task.save()<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f: \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e SQL\u2011\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432,\u00a0\u043b\u0438\u0448\u043d\u044f\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u0434\u043e\u043b\u0433\u0438\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434:<\/p>\n<pre><code class=\"python\">Task.objects.filter(progress_percent__gte=100).update(status='completed')<\/code><\/pre>\n<h3>SQL-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 Func()<\/h3>\n<p>\u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432\u00a0\u0432\u0435\u0440\u0445\u043d\u0435\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0435? \u0411\u0435\u0437\u00a0\u043f\u0440\u043e\u0431\u043b\u0435\u043c:<\/p>\n<pre><code class=\"python\">from django.db.models import Func, F  User.objects.annotate(name_upper=Func(F('name'), function='UPPER'))<\/code><\/pre>\n<p>\u0410\u00a0\u0435\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0431\u0440\u0430\u0442\u044c <code>full_name<\/code>:<\/p>\n<pre><code class=\"python\">from django.db.models import Func, F, Value  User.objects.annotate(     full_name=Func(         F('first_name'),         Value(' '),         F('last_name'),         function='CONCAT'     ) )<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 SQL. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 <code>.join()<\/code> \u0432\u00a0Python, \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u0445.<\/p>\n<h3>\u0410\u0432\u0442\u043e\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 updated_at \u0447\u0435\u0440\u0435\u0437 Now()<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c <code>updated_at<\/code>, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c <code>timezone.now()<\/code> \u0432\u00a0Python:<\/p>\n<pre><code class=\"python\">from django.db.models.functions import Now  MyModel.objects.filter(condition=True).update(updated_at=Now())<\/code><\/pre>\n<p>\u042d\u0442\u043e <code>NOW()<\/code> \u0432\u00a0SQL. \u0423\u0434\u043e\u0431\u043d\u043e, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 <code>select_for_update<\/code> \u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c.<\/p>\n<h3>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 SQL-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 Func()<\/h3>\n<p>Django \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>Func<\/code>, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 SQL:<\/p>\n<pre><code class=\"python\">from django.db.models import Func, IntegerField  class ExtractMonth(Func):     function = 'EXTRACT'     template = \"%(function)s(MONTH FROM %(expressions)s)\"     output_field = IntegerField()  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435: Invoice.objects.annotate(month=ExtractMonth('created_at'))<\/code><\/pre>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u00a0PostgreSQL, MySQL, Oracle\u00a0\u2014 \u0432\u0435\u0437\u0434\u0435, \u0433\u0434\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f <code>EXTRACT<\/code>.<\/p>\n<h3>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043a\u0438<\/h3>\n<p><strong>1. \u0411\u0435\u0437 <\/strong><code><strong>output_field<\/strong><\/code><strong> \u0432 <\/strong><code><strong>ExpressionWrapper<\/strong><\/code><strong>\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043e\u0448\u0438\u0431\u043a\u0443.<\/strong><\/p>\n<p>Django \u043d\u0435\u00a0\u0443\u043c\u0435\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 <code>annotate(F() + 1)<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443:<\/p>\n<pre><code class=\"python\">ExpressionWrapper(F('a') * F('b'), output_field=DecimalField())<\/code><\/pre>\n<p>\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d.<\/p>\n<p><strong>2. F() \u043d\u0435\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f.<\/strong><\/p>\n<p>\u0412\u00a0DRF \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>F('views') + 1<\/code> \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443. \u042d\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0430\u00a0\u043d\u0435\u00a0\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 <code>.filter()<\/code>, <code>.update()<\/code>, <code>.annotate()<\/code>.<\/p>\n<p><strong>3. SQL\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442\u00a0\u0431\u0430\u0437\u044b.<\/strong><\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432\u00a0PostgreSQL, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0432\u00a0SQLite. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 SQLite \u0432\u00a0\u0442\u0435\u0441\u0442\u0430\u0445\u00a0\u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443.<\/p>\n<h3>\u041a\u0435\u0439\u0441<\/h3>\n<p>\u0412\u00a0CRM\u2011\u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c KPI \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432 \u043f\u043e\u00a0\u0437\u0430\u043a\u0430\u0437\u0430\u043c:<\/p>\n<ul>\n<li>\n<p><code>score = orders_count <em> 2 + late_orders <\/em> -3 + F('bonus')<\/code><\/p>\n<\/li>\n<li>\n<p><code>status = \"top\"<\/code> \u0435\u0441\u043b\u0438 <code>score &gt; 50<\/code>, \u0438\u043d\u0430\u0447\u0435 <code>\"regular\"<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">Manager.objects.annotate(     score=ExpressionWrapper(         F('orders_count') * 2 + F('late_orders') * -3 + F('bonus'),         output_field=IntegerField()     ) ).update(     status=Case(         When(score__gt=50, then=Value('top')),         default=Value('regular'),         output_field=CharField()     ) )<\/code><\/pre>\n<p>\u0412\u0435\u0441\u044c \u043f\u0435\u0440\u0435\u0441\u0447\u0451\u0442\u00a0\u2014 <strong>\u043e\u0434\u043d\u0438\u043c SQL\u2011\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c<\/strong>. \u0411\u0435\u0437\u00a0\u0444\u043e\u043d\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0411\u0435\u0437\u00a0\u0443\u0442\u0435\u0447\u0435\u043a. <\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 select_for_update, Window, Subquery<\/h3>\n<p><strong>F() \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/strong><\/p>\n<pre><code class=\"python\">with transaction.atomic():     obj = Product.objects.select_for_update().get(id=1)     obj.stock = F('stock') - 1     obj.save()<\/code><\/pre>\n<p><strong>F() + Window\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/strong><\/p>\n<pre><code class=\"python\">from django.db.models import Window from django.db.models.functions import Rank  Leaderboard.objects.annotate(     rank=Window(         expression=Rank(),         order_by=F('score').desc()     ) )<\/code><\/pre>\n<p><strong>F() + Subquery:<\/strong><\/p>\n<pre><code class=\"python\">from django.db.models import Subquery, OuterRef  last_price = PriceHistory.objects.filter(     product_id=OuterRef('pk') ).order_by('-date').values('price')[:1]  Product.objects.annotate(last_price=Subquery(last_price))<\/code><\/pre>\n<hr\/>\n<h3>\ud83e\uddfe \u0418\u0442\u043e\u0433\u043e<\/h3>\n<p>\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b <code>F()<\/code> \u0438 <code>Func()<\/code>:<\/p>\n<ul>\n<li>\n<p><strong>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u044c.<\/strong> \u041e\u0434\u0438\u043d SQL\u00a0\u2014 \u043e\u0434\u043d\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435. \u0411\u0435\u0437\u00a0\u0433\u043e\u043d\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong> \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043c, \u0433\u0434\u0435 \u0436\u0438\u0432\u0443\u0442 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong> \u041f\u0438\u0448\u0435\u0442\u0441\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u043e, \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c.<\/strong> \u0418\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0441\u0431\u043e\u0438 \u043f\u0440\u0438\u00a0\u043c\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u043e\u0441\u0442\u044c.<\/strong> \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, <code>Case<\/code>, <code>Window<\/code>, <code>Subquery<\/code>.<\/p>\n<\/li>\n<\/ul>\n<hr\/>\n<p>\u041d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u0435:<\/p>\n<p><strong>23 \u0430\u043f\u0440\u0435\u043b\u044f, 20:00<\/strong><br \/>\u00ab<strong>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 Django \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435: \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 ORM \u0438 views\u00bb<\/strong><\/p>\n<p>\u042d\u0442\u043e\u0442 \u0443\u0440\u043e\u043a \u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u044b \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432 Django. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/iaSu\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 \u00abDjango\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00bb.<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/900796\/\"> https:\/\/habr.com\/ru\/articles\/900796\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/strong><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>F()<\/code>\u2011\u044d\u043a\u0441\u043f\u0440\u0435\u0441\u0441\u0438\u0438 \u0438 <code>Func()<\/code>\u2011\u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0432\u00a0Django \u0434\u043b\u044f\u00a0\u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0443, \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong>\u043d\u0435\u00a0\u0432\u00a0Python, \u0430\u00a0\u043d\u0430\u00a0\u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/strong>. \u042d\u0442\u043e \u0432\u0430\u0436\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0440\u0435\u0447\u044c \u0438\u0434\u0451\u0442 \u043e:<\/p>\n<ul>\n<li>\n<p>\u043c\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\u0445 \u0431\u0435\u0437 <code>save()<\/code>,<\/p>\n<\/li>\n<li>\n<p>\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u0438,<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u00a0\u0432\u044b\u0441\u043e\u043a\u0438\u0445 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430\u0445,<\/p>\n<\/li>\n<li>\n<p>\u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u00a0\u2014 \u0443\u043c\u0435\u043d\u0438\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u0432\u00a0\u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 SQL, \u043d\u0435\u00a0\u0442\u0435\u0440\u044f\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 Django ORM.<\/p>\n<\/li>\n<\/ul>\n<h3>F(): \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u043e\u043b\u044f<\/h3>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u00a0\u2014 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u0435 <code>views<\/code>:<\/p>\n<pre><code class=\"python\">article = Article.objects.get(id=1) article.views += 1 article.save()<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043c\u0435\u0436\u0434\u0443 <code>get()<\/code> \u0438 <code>save()<\/code> \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0433\u043e\u043d\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u00a0\u2014 \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u043e\u0432 \u043f\u043e\u0442\u0435\u0440\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<pre><code class=\"python\">from django.db.models import F  Article.objects.filter(id=1).update(views=F('views') + 1)<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u00a0SQL \u0432\u0438\u0434\u0430 <code>UPDATE article SET views = views + 1 WHERE id = 1<\/code>. \u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u0433\u043e\u043d\u043a\u0438. \u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430\u00a0\u043f\u0430\u043c\u044f\u0442\u044c. <\/p>\n<h3>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044f\u043c\u0438<\/h3>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0442\u043e\u0432\u0430\u0440\u044b, \u0443\u00a0\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0446\u0435\u043d\u0430 \u0432\u044b\u0448\u0435 \u0441\u043a\u0438\u0434\u043a\u0438:<\/p>\n<pre><code class=\"python\">Product.objects.filter(price__gt=F('discount_price'))<\/code><\/pre>\n<p>\u0411\u0430\u0437\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u044f\u043c\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f. \u041d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u00a0Python, \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u00a0\u043b\u044e\u0431\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438: \u0447\u0438\u0441\u043b\u0430, \u0434\u0430\u0442\u044b, \u0431\u0443\u043b\u0435\u0432\u044b, \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<h3>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u043e\u043b\u044f \u0447\u0435\u0440\u0435\u0437 annotate()<\/h3>\n<p>\u041f\u0443\u0441\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u0432\u00a0\u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043d\u0430\u00a0\u0441\u043a\u043b\u0430\u0434\u0435:<\/p>\n<pre><code class=\"python\">from django.db.models import F, ExpressionWrapper, DecimalField  Product.objects.annotate(     stock_value=ExpressionWrapper(         F('price') * F('stock'),         output_field=DecimalField()     ) )<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 DRF, <code>stock_value<\/code> \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0435 \u043a\u0430\u043a\u00a0\u043f\u043e\u043b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438\u00a0\u2014 \u0431\u0435\u0437\u00a0\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<h3>\u0423\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f: Case \/ When<\/h3>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0435\u0441\u043b\u0438 <code>stock == 0<\/code>, \u0445\u043e\u0442\u0438\u043c \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 <code>\"\u041d\u0435\u0442 \u0432 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\"<\/code>, \u0438\u043d\u0430\u0447\u0435 <code>\"\u0412 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\"<\/code>:<\/p>\n<pre><code class=\"python\">from django.db.models import Case, When, Value, CharField  Product.objects.annotate(     availability=Case(         When(stock=0, then=Value(\"\u041d\u0435\u0442 \u0432 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\")),         default=Value(\"\u0412 \u043d\u0430\u043b\u0438\u0447\u0438\u0438\"),         output_field=CharField()     ) )<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441\u2011\u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u044f\u043c\u043e \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440, \u0431\u0435\u0437\u00a0\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u00a0Python.<\/p>\n<h3>\u041c\u0430\u0441\u0441\u043e\u0432\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0412\u00a0\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0435\u0439\u0441: \u0435\u0441\u043b\u0438 <code>progress_percent &gt;= 100<\/code>, \u0442\u043e <code>status = 'completed'<\/code>.<\/p>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u0443\u0447\u0448\u0435 \u0437\u0430\u0431\u044b\u0442\u044c:<\/p>\n<pre><code class=\"python\">for task in Task.objects.all():     if task.progress_percent &gt;= 100:         task.status = 'completed'         task.save()<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f: \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e SQL\u2011\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432,\u00a0\u043b\u0438\u0448\u043d\u044f\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u0434\u043e\u043b\u0433\u0438\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434:<\/p>\n<pre><code class=\"python\">Task.objects.filter(progress_percent__gte=100).update(status='completed')<\/code><\/pre>\n<h3>SQL-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 Func()<\/h3>\n<p>\u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432\u00a0\u0432\u0435\u0440\u0445\u043d\u0435\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0435? \u0411\u0435\u0437\u00a0\u043f\u0440\u043e\u0431\u043b\u0435\u043c:<\/p>\n<pre><code class=\"python\">from django.db.models import Func, F  User.objects.annotate(name_upper=Func(F('name'), function='UPPER'))<\/code><\/pre>\n<p>\u0410\u00a0\u0435\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0431\u0440\u0430\u0442\u044c <code>full_name<\/code>:<\/p>\n<pre><code class=\"python\">from django.db.models import Func, F, Value  User.objects.annotate(     full_name=Func(         F('first_name'),         Value(' '),         F('last_name'),         function='CONCAT'     ) )<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 SQL. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 <code>.join()<\/code> \u0432\u00a0Python, \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u0445.<\/p>\n<h3>\u0410\u0432\u0442\u043e\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 updated_at \u0447\u0435\u0440\u0435\u0437 Now()<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c <code>updated_at<\/code>, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c <code>timezone.now()<\/code> \u0432\u00a0Python:<\/p>\n<pre><code class=\"python\">from django.db.models.functions import Now  MyModel.objects.filter(condition=True).update(updated_at=Now())<\/code><\/pre>\n<p>\u042d\u0442\u043e <code>NOW()<\/code> \u0432\u00a0SQL. \u0423\u0434\u043e\u0431\u043d\u043e, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 <code>select_for_update<\/code> \u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c.<\/p>\n<h3>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 SQL-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 Func()<\/h3>\n<p>Django \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>Func<\/code>, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 SQL:<\/p>\n<pre><code class=\"python\">from django.db.models import Func, IntegerField  class ExtractMonth(Func):     function = 'EXTRACT'     template = \"%(function)s(MONTH FROM %(expressions)s)\"     output_field = IntegerField()  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435: Invoice.objects.annotate(month=ExtractMonth('created_at'))<\/code><\/pre>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u00a0PostgreSQL, MySQL, Oracle\u00a0\u2014 \u0432\u0435\u0437\u0434\u0435, \u0433\u0434\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f <code>EXTRACT<\/code>.<\/p>\n<h3>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043a\u0438<\/h3>\n<p><strong>1. \u0411\u0435\u0437 <\/strong><code><strong>output_field<\/strong><\/code><strong> \u0432 <\/strong><code><strong>ExpressionWrapper<\/strong><\/code><strong>\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043e\u0448\u0438\u0431\u043a\u0443.<\/strong><\/p>\n<p>Django \u043d\u0435\u00a0\u0443\u043c\u0435\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 <code>annotate(F() + 1)<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443:<\/p>\n<pre><code class=\"python\">ExpressionWrapper(F('a') * F('b'), output_field=DecimalField())<\/code><\/pre>\n<p>\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d.<\/p>\n<p><strong>2. F() \u043d\u0435\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f.<\/strong><\/p>\n<p>\u0412\u00a0DRF \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>F('views') + 1<\/code> \u0432\u00a0\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443. \u042d\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0430\u00a0\u043d\u0435\u00a0\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 <code>.filter()<\/code>, <code>.update()<\/code>, <code>.annotate()<\/code>.<\/p>\n<p><strong>3. SQL\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442\u00a0\u0431\u0430\u0437\u044b.<\/strong><\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432\u00a0PostgreSQL, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0432\u00a0SQLite. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 SQLite \u0432\u00a0\u0442\u0435\u0441\u0442\u0430\u0445\u00a0\u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443.<\/p>\n<h3>\u041a\u0435\u0439\u0441<\/h3>\n<p>\u0412\u00a0CRM\u2011\u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c KPI \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432 \u043f\u043e\u00a0\u0437\u0430\u043a\u0430\u0437\u0430\u043c:<\/p>\n<ul>\n<li>\n<p><code>score = orders_count <em> 2 + late_orders <\/em> -3 + F('bonus')<\/code><\/p>\n<\/li>\n<li>\n<p><code>status = \"top\"<\/code> \u0435\u0441\u043b\u0438 <code>score &gt; 50<\/code>, \u0438\u043d\u0430\u0447\u0435 <code>\"regular\"<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">Manager.objects.annotate(     score=ExpressionWrapper(         F('orders_count') * 2 + F('late_orders') * -3 + F('bonus'),         output_field=IntegerField()     ) ).update(     status=Case(         When(score__gt=50, then=Value('top')),         default=Value('regular'),         output_field=CharField()     ) )<\/code><\/pre>\n<p>\u0412\u0435\u0441\u044c \u043f\u0435\u0440\u0435\u0441\u0447\u0451\u0442\u00a0\u2014 <strong>\u043e\u0434\u043d\u0438\u043c SQL\u2011\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c<\/strong>. \u0411\u0435\u0437\u00a0\u0444\u043e\u043d\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0411\u0435\u0437\u00a0\u0443\u0442\u0435\u0447\u0435\u043a. <\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 select_for_update, Window, Subquery<\/h3>\n<p><strong>F() \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/strong><\/p>\n<pre><code class=\"python\">with transaction.atomic():     obj = Product.objects.select_for_update().get(id=1)     obj.stock = F('stock') - 1     obj.save()<\/code><\/pre>\n<p><strong>F() + Window\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/strong><\/p>\n<pre><code class=\"python\">from django.db.models import Window from django.db.models.functions import Rank  Leaderboard.objects.annotate(     rank=Window(         expression=Rank(),         order_by=F('score').desc()     ) )<\/code><\/pre>\n<p><strong>F() + Subquery:<\/strong><\/p>\n<pre><code class=\"python\">from django.db.models import Subquery, OuterRef  last_price = PriceHistory.objects.filter(     product_id=OuterRef('pk') ).order_by('-date').values('price')[:1]  Product.objects.annotate(last_price=Subquery(last_price))<\/code><\/pre>\n<hr\/>\n<h3>\ud83e\uddfe \u0418\u0442\u043e\u0433\u043e<\/h3>\n<p>\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b <code>F()<\/code> \u0438 <code>Func()<\/code>:<\/p>\n<ul>\n<li>\n<p><strong>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u044c.<\/strong> \u041e\u0434\u0438\u043d SQL\u00a0\u2014 \u043e\u0434\u043d\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435. \u0411\u0435\u0437\u00a0\u0433\u043e\u043d\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong> \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043c, \u0433\u0434\u0435 \u0436\u0438\u0432\u0443\u0442 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong> \u041f\u0438\u0448\u0435\u0442\u0441\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u043e, \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c.<\/strong> \u0418\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0441\u0431\u043e\u0438 \u043f\u0440\u0438\u00a0\u043c\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u043e\u0441\u0442\u044c.<\/strong> \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, <code>Case<\/code>, <code>Window<\/code>, <code>Subquery<\/code>.<\/p>\n<\/li>\n<\/ul>\n<hr\/>\n<p>\u041d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u0435:<\/p>\n<p><strong>23 \u0430\u043f\u0440\u0435\u043b\u044f, 20:00<\/strong><br \/>\u00ab<strong>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 Django \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435: \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 ORM \u0438 views\u00bb<\/strong><\/p>\n<p>\u042d\u0442\u043e\u0442 \u0443\u0440\u043e\u043a \u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u044b \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432 Django. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/iaSu\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 \u00abDjango\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00bb.<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/900796\/\"> https:\/\/habr.com\/ru\/articles\/900796\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-457999","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/457999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=457999"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/457999\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=457999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=457999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=457999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}