{"id":269792,"date":"2015-12-07T17:19:02","date_gmt":"2015-12-07T14:19:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=269792"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=269792","title":{"rendered":"Django: \u041a\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u043c QuerySet"},"content":{"rendered":"<p>       \u0412\u0441\u0435\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e!<\/p>\n<p>  \u0422\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0431\u0430\u0433 (\u0431\u0430\u0433 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438, \u043d\u043e \u043d\u0435 \u043c\u0430\u0448\u0438\u043d\u044b), \u0438 \u0440\u0435\u0448\u0438\u043b \u0438\u043c \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e\u043c. \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u0443\u044e \u043d\u0430 django \u0443\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0433\u043e, \u043d\u043e \u0441 \u0442\u0430\u043a\u0438\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435, \u0442\u0430\u043a \u0447\u0442\u043e, \u0434\u0443\u043c\u0430\u044e, \u043a\u043e\u043c\u0443-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0430 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f. \u0427\u0442\u043e \u0436, \u043a \u0434\u0435\u043b\u0443!<\/p>\n<p>  \u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0432 \u043a\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a:  <\/p>\n<pre><code class=\"python\"># views.py ids = [5201, 5230, 5183, 5219, 5217, 5209, 5246, 5252, 5164, 5248, ...&lt;\u0438 \u0442.\u0434.&gt;...] products = Product.objects.filter(id__in=ids) <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u044b \u043f\u0440\u043e \u043f\u043e\u043c\u043e\u0449\u0438 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0435 \u043f\u043e 20 \u0448\u0442\u0443\u043a. \u041e\u0434\u043d\u0430\u0436\u0434\u044b \u0437\u0432\u043e\u043d\u0438\u0442 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u0442\u043e\u0432\u0430\u0440 \u00ab\u043f\u0440\u044b\u0433\u0430\u0435\u0442\u00bb \u043f\u043e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c \u2014 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043d \u0431\u044b\u043b \u0437\u0430\u043c\u0435\u0447\u0435\u043d \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435, \u0430 \u043f\u043e\u0442\u043e\u043c \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u044f\u0442\u043e\u0439.<\/p>\n<p>  \u00ab\u0425\u0430\u00bb \u2014 \u0437\u0430\u044f\u0432\u043b\u044f\u0435\u043c \u043c\u044b, \u0441\u0442\u0430\u0432\u0438\u043c \u0431\u0440\u0435\u0439\u043a\u043f\u043e\u0438\u043d\u0442 \u043f\u043e\u0441\u043b\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c print(products). \u0412\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e \u0438, \u0434\u043b\u044f \u0432\u0435\u0440\u043d\u043e\u0441\u0442\u0438, \u0446\u0438\u043a\u043b\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0432\u044b\u0432\u043e\u0434 \u2014 \u0430 \u0442\u0430\u043c \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u043d\u0435\u0442!<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<p>  \u0421\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u043e\u0442 \u0447\u0442\u043e: \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0442\u043b\u043e\u0432\u0438\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u0432\u0430\u0440 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438 \u0441\u043b\u0430\u0439\u0441\u0430\u043c\u0438. \u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0432\u0430\u0435\u043c \u043d\u0435\u0433\u043e\u0434\u044f\u0435\u0432: products[3] == products[20]. \u0422\u0430\u043a, \u043d\u0430\u0448\u043b\u0438 \u0438\u0445. 3 \u0438 20. \u0422\u043e\u0432\u0430\u0440 <i>name<\/i>.<\/p>\n<p>  \u0412\u044b\u0432\u043e\u0434\u0438\u043c: print(products), \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 3 \u0438 20\u2026 \u0430 \u0442\u0430\u043c \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u044b! \u0414\u0430 \u043a\u0430\u043a \u0442\u0430\u043a?<\/p>\n<p>  \u041f\u0440\u043e\u0431\u0443\u0435\u043c print(products[0:10]) \u2014 \u0442\u043e\u0432\u0430\u0440 \u0432 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 3 \u0435\u0441\u0442\u044c \u2014 <i>name<\/i>. \u041f\u0440\u043e\u0431\u0443\u0435\u043c print(products[10:21]) \u2014 \u0442\u043e\u0432\u0430\u0440 \u0432 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 20 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442\u044c, \u0438 \u043e\u043d \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u2014 <i>name<\/i>. @#! \u041d\u0443 \u0447\u0442\u043e \u0436, \u0432\u0438\u0434\u0438\u043c\u043e, django \u043a\u0430\u043a-\u0442\u043e \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u0434\u0435\u043b\u0430\u0435\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u0438 \u0432\u0437\u044f\u0442\u0438\u0435 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 (\u0448\u0442\u043e\u0430?), \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c.<\/p>\n<p>  \u041b\u0435\u0437\u0438\u043c \u0432 QuerySet \u043a\u043b\u0430\u0441\u0441, \u0442\u0430\u043c \u0441\u043c\u043e\u0442\u0440\u0438\u043c __getitem__ \u043c\u0435\u0442\u043e\u0434, \u0432\u043e\u0442 \u043e\u043d \u0432 \u043a\u0440\u0430\u0442\u043a\u043e\u043c \u043f\u0435\u0440\u0435\u0441\u043a\u0430\u0437\u0435:  <\/p>\n<pre><code class=\"python\">qs = self._clone() qs.query.set_limits(k, k + 1) return list(qs)[0] <\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u0432\u0437\u044f\u0442\u0438\u0435 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 set_limits \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043a\u0430\u043a \u0436\u0435 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432 SQL \u2014 \u043c\u043e\u0436\u0435\u0442, \u0442\u0443\u0434\u0430 \u0437\u0430\u043a\u0440\u0430\u043b\u0430\u0441\u044c \u043e\u0448\u0438\u0431\u043a\u0430?<\/p>\n<pre><code class=\"python\">qs1 = products._clone() qs1.query.set_limits(3, 4) print(qs1.query)  qs2 = products._clone() qs2.query.set_limits(20, 21) print(qs2.query) <\/code><\/pre>\n<p>  \u0418 \u043a\u043e\u0433\u0434\u0430 \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b  <\/p>\n<pre><code class=\"sql\">SELECT &quot;shop_product&quot;.&quot;id&quot;, ... FROM &quot;shop_product&quot; WHERE (&quot;shop_product&quot;.&quot;id&quot; IN (5201, 5230, 5183, 5219, 5217, 5209, 5246, 5252, 5164, 5248)) LIMIT 1 OFFSET 3 <\/code><\/pre>\n<p>  \u0438  <\/p>\n<pre><code class=\"sql\">SELECT &quot;shop_product&quot;.&quot;id&quot;, ... FROM &quot;shop_product&quot; WHERE (&quot;shop_product&quot;.&quot;id&quot; IN (5201, 5230, 5183, 5219, 5217, 5209, 5246, 5252, 5164, 5248)) LIMIT 1 OFFSET 20, <\/code><\/pre>\n<p>  \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u043b. \u041f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0432 \u0431\u0430\u0437\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435 \u0437\u0430\u043f\u0438\u0441\u044c? \u041d\u043e \u0442\u0430\u043c \u0436\u0435 constraint \u043d\u0430 id, \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u0431\u044b\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u2026<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b \u0440\u0443\u0447\u043a\u0430\u043c\u0438 \u043e\u0431\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u0440\u044f\u043c\u043e \u0432 <b>Postgresql<\/b> \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u044f \u043d\u0430\u0447\u0430\u043b \u0433\u0443\u0433\u043b\u0438\u0442\u044c \u043f\u043e postgres limit offset duplicates \u0438 \u043d\u0430\u0448\u0451\u043b <a href=\"http:\/\/stackoverflow.com\/questions\/13580826\/postgresql-repeating-rows-from-limit-offset\">\u043e\u0442\u0432\u0435\u0442 \u043d\u0430 stackoverflow<\/a>. \u0410 \u0448\u0442\u0443\u043a\u0430 \u0432\u043e\u0442 \u043a\u0430\u043a\u0430\u044f:<\/p>\n<p>  \u041a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u043d \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u0442\u0440\u043e\u043a \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 (ORDER_BY), \u0442\u043e Postgres \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c <b>\u043b\u044e\u0431\u0443\u044e<\/b> \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0435\u043c\u0443 \u043f\u043e \u0434\u0443\u0448\u0435 \u2014 \u0438 \u044f, \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e, \u043d\u0435 \u043f\u0440\u043e\u0442\u0438\u0432, \u044f \u0436\u0435 \u0442\u0430\u043a \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043b: Product.objects.filter(&#8230;), \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u0438\u0445 order_by(). \u041a\u043e\u0433\u0434\u0430 \u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0438\u0441\u0430\u043b \u044d\u0442\u043e\u0442 \u043a\u043e\u0434, \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u043d\u0435 \u0431\u044b\u043b\u043e, \u0438 \u0432\u0441\u0435 \u0442\u043e\u0432\u0430\u0440\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u0440\u0430\u0437\u043e\u043c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u2014 \u0442\u0443\u0442 Postgres \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0441\u0435 \u044d\u0442\u0438 \u0442\u043e\u0432\u0430\u0440\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e, \u043d\u043e \u0437\u0430\u0442\u043e \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443.<\/p>\n<p>  \u0410 \u043f\u043e\u0442\u043e\u043c, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0440\u0430\u0437\u0431\u0438\u0432\u043a\u0430 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b, \u0431\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u043b\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043d\u0430\u0432\u0440\u043e\u0434\u0435 \u00ab\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u043a\u0430\u043a \u0442\u0435\u0431\u0435 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0438 \u0434\u0430\u0439 \u043c\u043d\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0441 20 \u043f\u043e 40\u00bb, \u0438 \u0432\u043e\u0442 \u043f\u0440\u0438 \u0440\u0430\u0437\u043d\u044b\u0445 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\u0445 (0-20 \u0438\u043b\u0438 20-40) \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0431\u044b\u043b\u0430 \u0440\u0430\u0437\u043d\u0430\u044f \u2014 \u044d\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043b\u043e \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 postgres \u2014 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u0432\u044b\u0432\u043e\u0434 \u0448\u043b\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430.<\/p>\n<p>  \u0410 \u0432\u043e\u0442 \u0438 \u0446\u0438\u0442\u0430\u0442\u0430 <a href=\"http:\/\/www.postgresql.org\/docs\/9.0\/static\/queries-limit.html\">\u0441 \u0441\u0430\u0439\u0442\u0430 postgres<\/a>:  <\/p>\n<blockquote><p>The query optimizer takes LIMIT into account when generating query plans, so you are very likely to get different plans (yielding different row orders) depending on what you give for LIMIT and OFFSET. Thus, using different LIMIT\/OFFSET values to select different subsets of a query result will give inconsistent results unless you enforce a predictable result ordering with ORDER BY. This is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular order unless ORDER BY is used to constrain the order.<\/p><\/blockquote>\n<p>  \u0427\u0442\u043e \u0436, \u0431\u0443\u0434\u0435\u043c \u0437\u043d\u0430\u0442\u044c!  <\/p>\n<pre><code class=\"python\">products = Product.objects.filter(...).order_by('price') <\/code><\/pre>\n<p>  \u0414\u0430? \u041d\u0415\u0422! \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0432 \u0432\u0441\u0451, \u044f \u043e\u043f\u044f\u0442\u044c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u0434\u0443\u0431\u043b\u0438 \u2014 \u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u044f \u0443\u0436\u0435 \u043f\u043e\u043f\u0430\u043b\u0441\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e order_by \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432 \u2014 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u0432\u0441\u0435\u0445 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0439 \u0446\u0435\u043d\u043e\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u043f\u044f\u0442\u044c \u0431\u044b\u043b \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d. \u0422\u0430\u043a \u0447\u0442\u043e:  <\/p>\n<pre><code class=\"python\">products = Products.objects.filter(...).order_by('price', 'id') <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u0442\u043e\u0447\u043d\u043e \u0432\u0441\u0451.<\/p>\n<p>  P.S. \u041b\u0438\u0447\u043d\u043e \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u0435\u0449\u0451 \u043e\u0434\u043d\u0438\u043c \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u044b\u043c \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435\u043c \u00ab\u0417\u0430\u043a\u043e\u043d\u0430 \u0434\u044b\u0440\u044f\u0432\u044b\u0445 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0439\u00bb \u2014 \u0442\u044b \u0432\u0440\u043e\u0434\u0435 \u043f\u0438\u0448\u0435\u0448\u044c ORM \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u00ab\u0434\u0430\u0439 \u043c\u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 20-40\u00bb, \u0438 \u0432\u0440\u043e\u0434\u0435 \u0434\u0430\u0436\u0435 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u043d\u0430\u0442\u044c SQL \u0438 Postgres, \u043d\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u0432 \u043e\u0434\u0438\u043d \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u044d\u0442\u0430 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f \u0442\u0435\u0447\u0451\u0442, \u0438 \u0432\u043e\u0442 \u0442\u044b \u0443\u0436\u0435 \u0438\u0437\u0443\u0447\u0430\u0435\u0448\u044c \u043e\u0441\u043d\u043e\u0432\u044b.               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/272559\/\"> http:\/\/habrahabr.ru\/post\/272559\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0412\u0441\u0435\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e!<\/p>\n<p>  \u0422\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0431\u0430\u0433 (\u0431\u0430\u0433 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438, \u043d\u043e \u043d\u0435 \u043c\u0430\u0448\u0438\u043d\u044b), \u0438 \u0440\u0435\u0448\u0438\u043b \u0438\u043c \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e\u043c. \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u0443\u044e \u043d\u0430 django \u0443\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0433\u043e, \u043d\u043e \u0441 \u0442\u0430\u043a\u0438\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435, \u0442\u0430\u043a \u0447\u0442\u043e, \u0434\u0443\u043c\u0430\u044e, \u043a\u043e\u043c\u0443-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0430 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f. \u0427\u0442\u043e \u0436, \u043a \u0434\u0435\u043b\u0443!<\/p>\n<p>  \u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0432 \u043a\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a:  <\/p>\n<pre><code class=\"python\"># views.py ids = [5201, 5230, 5183, 5219, 5217, 5209, 5246, 5252, 5164, 5248, ...&lt;\u0438 \u0442.\u0434.&gt;...] products = Product.objects.filter(id__in=ids) <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u044b \u043f\u0440\u043e \u043f\u043e\u043c\u043e\u0449\u0438 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0435 \u043f\u043e 20 \u0448\u0442\u0443\u043a. \u041e\u0434\u043d\u0430\u0436\u0434\u044b \u0437\u0432\u043e\u043d\u0438\u0442 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u0442\u043e\u0432\u0430\u0440 \u00ab\u043f\u0440\u044b\u0433\u0430\u0435\u0442\u00bb \u043f\u043e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c \u2014 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043d \u0431\u044b\u043b \u0437\u0430\u043c\u0435\u0447\u0435\u043d \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435, \u0430 \u043f\u043e\u0442\u043e\u043c \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u044f\u0442\u043e\u0439.<\/p>\n<p>  \u00ab\u0425\u0430\u00bb \u2014 \u0437\u0430\u044f\u0432\u043b\u044f\u0435\u043c \u043c\u044b, \u0441\u0442\u0430\u0432\u0438\u043c \u0431\u0440\u0435\u0439\u043a\u043f\u043e\u0438\u043d\u0442 \u043f\u043e\u0441\u043b\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c print(products). \u0412\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e \u0438, \u0434\u043b\u044f \u0432\u0435\u0440\u043d\u043e\u0441\u0442\u0438, \u0446\u0438\u043a\u043b\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0432\u044b\u0432\u043e\u0434 \u2014 \u0430 \u0442\u0430\u043c \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u043d\u0435\u0442!<\/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-269792","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269792","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=269792"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269792\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}