{"id":298304,"date":"2020-02-06T15:00:11","date_gmt":"2020-02-06T15:00:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=298304"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=298304","title":{"rendered":"\u041e\u043f\u044b\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f web-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 Pony ORM"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/487296\/\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>  <\/p>\n<p>\u041d\u0435\u0434\u0430\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0432\u0441\u0442\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 Python web-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u0447\u0451\u0442\u0430 \u0432 \u0440\u0435\u0441\u0442\u043e\u0440\u0430\u043d\u0435 \u043c\u0435\u0436\u0434\u0443 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0430\u043c\u0438 \u0442\u0440\u0430\u043f\u0435\u0437\u044b. \u0422\u0430\u043a \u043a\u0430\u043a \u043d\u0443\u0436\u043d\u0430 DB \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043e \u0437\u0430\u043a\u0430\u0437\u0430\u0445 \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u0445, \u0432\u0441\u0442\u0430\u043b \u0432\u043e\u043f\u0440\u043e\u0441 \u0432\u044b\u0431\u043e\u0440\u0430 ORM \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439. \u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u0435\u043b\u0430\u0441\u044c \u043d\u0430 Flask, \u0442\u0430\u043a \u0447\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u043c\u0435\u0442\u0430\u0435\u0442\u0441\u044f Django ORM \u0438 \u0432\u044b\u0431\u043e\u0440 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043f\u0430\u043b \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 SQLAlchemy. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u044d\u0442\u0430 ORM \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u043c\u043e\u0433\u0443\u0449\u0430\u044f, \u043d\u043e \u0437\u0430 \u0441\u0447\u0435\u0442 \u044d\u0442\u043e\u0433\u043e \u043e\u043d\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u044f\u0436\u0435\u043b\u0430 \u0432 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u0438. \u041f\u043e\u043c\u0443\u0447\u0438\u0432\u0448\u0438\u0441\u044c \u0441 \u0430\u043b\u0445\u0438\u043c\u0438\u0435\u0439 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u0439\u0442\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0447\u0442\u043e\u0431 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0448\u043b\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0431\u044b\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u0430 Pony ORM.<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/66e\/1b5\/bc8\/66e1b5bc87a841c9150d9470081f52cc.jpg\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u0412 \u0433\u043b\u0430\u0437\u0430 \u0441\u0440\u0430\u0437\u0443 \u0431\u0440\u043e\u0441\u0438\u043b\u043e\u0441\u044c \u0442\u043e, \u0447\u0442\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u043c\u0435\u043d\u0435\u0435 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0439, \u0447\u0435\u043c \u0432 \u0430\u043b\u0445\u0438\u043c\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u043d\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0443\u0445\u043e\u0434\u0438\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430. <\/p>\n<p>  <\/p>\n<p>\u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043e\u0431\u0437\u043e\u0440\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e, \u0447\u0442\u043e\u0431 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u041f\u043e\u043d\u0438. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043e\u043d\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0445 \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p><b>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 PonyORM:<\/b><\/p>\n<p>  <\/p>\n<ul>\n<li> \u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0438\u0442\u043e\u043d\u043e\u0432\u0441\u043a\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 (\u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u043b\u0438 lambda \u0444\u0443\u043d\u043a\u0446\u0438\u0438)<\/li>\n<li>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/li>\n<li>\u041f\u043e\u043b\u043d\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0442\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u044f \u0431\u044b \u0432\u044b\u0434\u0435\u043b\u0438\u043b \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 (\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430\u0434 \u044d\u0442\u0438\u043c)<\/p>\n<p>  <\/p>\n<p>\u0412\u0437\u044f\u0432 \u0432 \u0440\u0430\u0441\u0447\u0435\u0442 \u0432\u0441\u0451 \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435, \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e Pony \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u0440\u043e\u043c\u0435 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u0447\u0430\u0441\u0442\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0445\u0435\u043c\u0443 \u0431\u0430\u0437\u044b \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Pony \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430 \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0432\u044b\u0448\u0435.<\/p>\n<p>  <\/p>\n<p><b>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/b><\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 pip \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c Pony:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">pip install pony<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c DB API \u0434\u043b\u044f \u043d\u0443\u0436\u043d\u043e\u0439 \u0421\u0423\u0411\u0414:<\/p>\n<p>  <\/p>\n<ul>\n<li>PostgreSQL: psycopg \u0438\u043b\u0438 psycopg2cffi <\/li>\n<li>Oracle: cx_Oracle <\/li>\n<li>MySQL: PyMySQL <\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 Pony:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">from pony.orm import *<\/code><\/pre>\n<p>  <\/p>\n<p><b>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0411\u0414 \u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0430 \u0435\u0433\u043e \u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0431\u0430\u0437\u0435<\/b><\/p>\n<p>  <\/p>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u044d\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0431\u0430\u0437\u044b \u0432 \u043f\u0438\u0442\u043e\u043d\u043e\u0432\u0441\u043a\u043e\u043c \u043a\u043e\u0434\u0435. \u0421 \u0431\u0430\u0437\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">db = Database()<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u043a \u0431\u0430\u0437\u0435. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 Database \u043b\u0438\u0431\u043e \u0432\u044b\u0437\u0432\u0430\u0432 \u043c\u0435\u0442\u043e\u0434 db.bind() \u0441 \u043d\u0443\u0436\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438. \u0414\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0421\u0423\u0411\u0414 \u043e\u043d\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 SQLite (\u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438) \u0438 PostgreSQL (\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d). \u0414\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0421\u0423\u0411\u0414 \u044f \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0445\u0440\u0430\u043d\u044e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u044e \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Database. \u0424\u0430\u0439\u043b \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443 \u043c\u0435\u043d\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">settings = dict(     sqlite=dict(provider='sqlite', filename='pony.db', create_db=True),     postgres=dict(provider='postgres', user='pony', password='pony', host='localhost',          database='pony') ) <\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0441\u0430\u043c\u0430 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u0430\u0437\u044b :<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">db = Database(**settings['postgres'])<\/code><\/pre>\n<p>  <\/p>\n<p><b>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/b><br \/>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0441\u0445\u0435\u043c\u0443 \u0431\u0430\u0437\u044b \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0435 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f ER \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u043f\u043e\u043d\u0438. \u0423\u0434\u043e\u0431\u0435\u043d \u043e\u043d \u0442\u0435\u043c, \u0447\u0442\u043e \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c Python \u043a\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u043e\u0439 \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0435 \u0441\u0445\u0435\u043c\u0435. \u0421\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u043d\u043e \u044f \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/29b\/da5\/734\/29bda57340afac2b5387dc305f7462b5.png\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0435\u0451 \u043e\u0441\u043d\u043e\u0432\u0435 \u0431\u044b\u043b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from datetime import datetime from pony.orm import *  db = Database()  class User(db.Entity):     id = PrimaryKey(int, auto=True)     fullname = Optional(str)     password = Optional(str)     nickname = Optional(str)     lended = Set('Credit', reverse='lender')     borrowed = Set('Credit', reverse='borrower')     credit_editions = Set('CreditEdition', reverse='user')     sessions = Set('UserInSession')     affected_editions = Set('CreditEdition', reverse='affected_user')  class Session(db.Entity):     id = PrimaryKey(int, auto=True)     title = Optional(str)     start = Optional(datetime)     end = Optional(datetime)     ordereders = Set('OrderedItem')     users = Set('UserInSession')  class OrderedItem(db.Entity):     id = PrimaryKey(int, auto=True)     session = Required(Session)     title = Optional(str)     price = Optional(int)     user_in_sessions = Set('UserInSession')  class Credit(db.Entity):     id = PrimaryKey(int, auto=True)     lender = Required(User, reverse='lended')     borrower = Required(User, reverse='borrowed')     value = Optional(int)     credit_editions = Set('CreditEdition')  class UserInSession(db.Entity):     id = PrimaryKey(int, auto=True)     user = Required(User)     session = Required(Session)     orders = Set(OrderedItem)     value = Optional(int)  class CreditEdition(db.Entity):     id = PrimaryKey(int, auto=True)     user = Required(User, reverse='credit_editions')     affected_user = Required(User, reverse='affected_editions')     credit = Required(Credit)     old_value = Optional(int)     new_value = Optional(int)  db.generate_mapping()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u043e\u0431\u0449\u0438\u0432, \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u0430 \u0432 \u0432\u043e\u043f\u0440\u043e\u0441\u0430\u0445 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u0430\u043c\u0438.<\/p>\n<p>  <\/p>\n<p><b>\u0417\u0430\u043f\u0440\u043e\u0441\u044b<\/b><br \/>  \u041e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u043f\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443 \u0432 \u043f\u043e\u043d\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 (\u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session = Session[sid]<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b Session \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 id == sid. <\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435, \u043a\u0430\u043a \u044f \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0432\u044b\u0448\u0435, \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 Pony \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u043c\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0438\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435, \u0447\u0435\u043c \u0432 \u0430\u043b\u0445\u0438\u043c\u0438\u0438.<br \/>  \u0421\u043d\u0438\u0437\u0443 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0430\u0437\u0435 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0435\u0441\u0441\u0438\u0438 \u0438 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">users = select(u for u in User if u not in session.users.user and not u.virtual)<\/code><\/pre>\n<p>  <\/p>\n<p><b>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/b><\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043c\u043e\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043a\u043e\u043c\u0443-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439. \u0415\u0441\u043b\u0438 \u0432\u0430\u0441 \u0442\u043e\u0436\u0435 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043b Pony, \u0432\u043d\u0438\u0437\u0443 \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0438 \u043d\u0430 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u043a\u0430\u043d\u0430\u043b \u043a\u043e\u043c\u044c\u044e\u043d\u0438\u0442\u0438. <\/p>\n<p>  <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/docs.ponyorm.org\/index.html\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f Pony<\/a><br \/>  <a href=\"https:\/\/telegram.me\/ponyorm\">\u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u043a\u043e\u043c\u043c\u044c\u044e\u043d\u0438\u0442\u0438<\/a><\/p>\n<\/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=\"https:\/\/habr.com\/ru\/post\/487296\/\"> https:\/\/habr.com\/ru\/post\/487296\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/487296\/\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>  <\/p>\n<p>\u041d\u0435\u0434\u0430\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0432\u0441\u0442\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 Python web-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u0447\u0451\u0442\u0430 \u0432 \u0440\u0435\u0441\u0442\u043e\u0440\u0430\u043d\u0435 \u043c\u0435\u0436\u0434\u0443 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0430\u043c\u0438 \u0442\u0440\u0430\u043f\u0435\u0437\u044b. \u0422\u0430\u043a \u043a\u0430\u043a \u043d\u0443\u0436\u043d\u0430 DB \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043e \u0437\u0430\u043a\u0430\u0437\u0430\u0445 \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u0445, \u0432\u0441\u0442\u0430\u043b \u0432\u043e\u043f\u0440\u043e\u0441 \u0432\u044b\u0431\u043e\u0440\u0430 ORM \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439. \u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u0435\u043b\u0430\u0441\u044c \u043d\u0430 Flask, \u0442\u0430\u043a \u0447\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u043c\u0435\u0442\u0430\u0435\u0442\u0441\u044f Django ORM \u0438 \u0432\u044b\u0431\u043e\u0440 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043f\u0430\u043b \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 SQLAlchemy. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u044d\u0442\u0430 ORM \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u043c\u043e\u0433\u0443\u0449\u0430\u044f, \u043d\u043e \u0437\u0430 \u0441\u0447\u0435\u0442 \u044d\u0442\u043e\u0433\u043e \u043e\u043d\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u044f\u0436\u0435\u043b\u0430 \u0432 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u0438. \u041f\u043e\u043c\u0443\u0447\u0438\u0432\u0448\u0438\u0441\u044c \u0441 \u0430\u043b\u0445\u0438\u043c\u0438\u0435\u0439 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u0439\u0442\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0447\u0442\u043e\u0431 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0448\u043b\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0431\u044b\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u0430 Pony ORM.<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/66e\/1b5\/bc8\/66e1b5bc87a841c9150d9470081f52cc.jpg\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u0412 \u0433\u043b\u0430\u0437\u0430 \u0441\u0440\u0430\u0437\u0443 \u0431\u0440\u043e\u0441\u0438\u043b\u043e\u0441\u044c \u0442\u043e, \u0447\u0442\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u043c\u0435\u043d\u0435\u0435 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0439, \u0447\u0435\u043c \u0432 \u0430\u043b\u0445\u0438\u043c\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u043d\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0443\u0445\u043e\u0434\u0438\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430. <\/p>\n<p>  <\/p>\n<p>\u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043e\u0431\u0437\u043e\u0440\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e, \u0447\u0442\u043e\u0431 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u041f\u043e\u043d\u0438. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043e\u043d\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0445 \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/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-298304","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/298304","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=298304"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/298304\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}