{"id":283921,"date":"2017-03-26T17:40:02","date_gmt":"2017-03-26T13:40:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=283921"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=283921","title":{"rendered":"\u041a\u0430\u043a \u044f SQLAlchemy \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0441\u0434\u0435\u043b\u0430\u043b"},"content":{"rendered":"<p>\u041d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e <a href=\"https:\/\/www.sqlalchemy.org\/\">SQLAlchemy<\/a> \u2014 \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f ORM \u043d\u0430 Python. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u0432\u0435\u0449\u0438, \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e Active Record \u0441\u043e\u0431\u0440\u0430\u0442\u044c\u0435\u0432. \u041d\u043e \u043f\u043b\u0430\u0442\u0430 \u0437\u0430 \u044d\u0442\u043e \u2014 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043a\u043e\u0434, \u0438 \u0432 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0432\u0440\u043e\u0434\u0435 CRUD \u044d\u0442\u043e \u043d\u0430\u043f\u0440\u044f\u0433\u0430\u0435\u0442.<\/p>\n<p>  <\/p>\n<p>\u041e \u0442\u043e\u043c, \u043a\u0430\u043a \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0410\u043b\u0445\u0438\u043c\u0438\u044e \u0443\u0434\u043e\u0431\u043d\u043e\u0439, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0432\u0448\u0438\u0441\u044c \u043e\u043f\u044b\u0442\u043e\u043c \u043b\u0443\u0447\u0448\u0438\u0445 Active Record ORM, \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/p>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u042f \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c SQLAlchemy, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0432 \u0441 <a href=\"http:\/\/rusrails.ru\/active-record-basics\">Active Record<\/a> \u0432 Ruby on Rails, c <a href=\"http:\/\/laravel.su\/docs\/5.0\/eloquent\">Eloquent ORM<\/a> \u0432 PHP \u0438 c <a href=\"https:\/\/docs.djangoproject.com\/en\/1.10\/topics\/db\/\">Django ORM<\/a> \u0438 <a href=\"https:\/\/habrahabr.ru\/post\/207110\/\">Peewee<\/a> \u0432 Python. \u0412\u0441\u0435 \u044d\u0442\u0438 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/ActiveRecord\">Active Record<\/a> ORM \u0438\u043c\u0435\u044e\u0442 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u0434, \u0438 \u043c\u043d\u0435 \u043e\u043d\u0438 \u043e\u0447\u0435\u043d\u044c \u043d\u0440\u0430\u0432\u044f\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u0410\u043b\u0445\u0438\u043c\u0438\u044f \u0442\u043e\u0436\u0435 \u0445\u043e\u0440\u043e\u0448\u0430: \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u043e\u0432\u0441\u0435\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0434\u0430 \u0438 \u043f\u0430\u0442\u0442\u0435\u0440\u043d <a href=\"http:\/\/designpatternsphp.readthedocs.io\/ru\/latest\/Structural\/DataMapper\/README.html\">Data Mapper<\/a> \u0440\u0443\u043b\u0438\u0442, \u043d\u043e \u043f\u043e\u0441\u043b\u0435 Active Record ORM \u043c\u043d\u043e\u0433\u043e\u0433\u043e \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442.<\/p>\n<p>  <\/p>\n<p>\u041c\u043d\u043e\u0433\u0438\u0435, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u0410\u043b\u0445\u0438\u043c\u0438\u0438 \u043d\u0435 \u0440\u0435\u0448\u0435\u043d\u044b:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0423\u0434\u043e\u0431\u043d\u044b\u0439 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/CRUD\">CRUD<\/a>: \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e <code>create<\/code> \u0432 \u0410\u043b\u0445\u0438\u043c\u0438\u0438 \u043d\u0430\u0434\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442, \u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/session_basics.html\">\u0441\u0435\u0441\u0441\u0438\u044e<\/a>, \u0434\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c flush<\/li>\n<li>\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\/\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <em>\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a<\/em> \u043a\u0430\u043a \u0432 <a href=\"https:\/\/docs.djangoproject.com\/en\/1.10\/topics\/db\/queries\/#lookups-that-span-relationships\">Django<\/a>: <code>Post.objects.filter(user__name__startswith='John')<\/code><\/li>\n<li>\u0412\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/tutorial.html?highlight=eager%20load#eager-loading\">eager load<\/a>, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043f\u043e\u0441\u0442, \u0430 \u043a \u043f\u043e\u0441\u0442\u0443 \u0435\u0433\u043e \u044e\u0437\u0435\u0440\u0430 (\u043b\u0430\u0434\u043d\u043e, \u043e\u043d <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/loading_relationships.html#sqlalchemy.orm.joinedload_all\">\u0435\u0441\u0442\u044c<\/a>, \u043d\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u0435\u043d) <\/li>\n<li>\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u044b\u0439 <code>__repr__<\/code>: <code>print(post)<\/code> \u0432\u044b\u0434\u0430\u0451\u0442 \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 <code>&lt;myapp.models.Post at 0x04287A50&gt;<\/code>, \u0434\u043b\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0438 \u044d\u0442\u043e \u043d\u0435 \u0433\u043e\u0434\u0438\u0442\u0441\u044f<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043d\u0430\u0441\u043b\u0430\u0436\u0434\u0435\u043d\u0438\u044f \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Active Record ORM, \u044f \u043d\u0435 \u043c\u043e\u0433 \u0441\u0442\u0435\u0440\u043f\u0435\u0442\u044c \u0438 \u0440\u0435\u0448\u0438\u043b \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>  <\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0444\u043e\u0440\u043c\u043b\u0435\u043d\u043e \u0432 <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins\">\u0445\u043e\u0440\u043e\u0448\u043e \u043e\u0442\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442<\/a>.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0415\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b!<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0414\u0430, \u0435\u0441\u0442\u044c, \u043d\u043e \u043e\u043d\u0438 \u043b\u0438\u0431\u043e \u0442\u044f\u0436\u0435\u043b\u043e \u0432\u043d\u0435\u0434\u0440\u044f\u044e\u0442\u0441\u044f, \u043b\u0438\u0431\u043e \u0437\u0430\u0442\u043e\u0447\u0435\u043d\u044b \u043f\u043e\u0434 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u043b\u0438\u0431\u043e \u043f\u043b\u043e\u0445\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b.<br \/>  \u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0438\u043c\u0435\u0442\u044c <em>\u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0435, \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u043e\u0435<\/em> \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439_\u043c\u043e\u0434\u0443\u043b\u044c import ActiveRecordMixin  class User(Base, ActiveRecordMixin):      pass<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438 \u0438\u043c\u0435\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 Active Record.<br \/>  \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b <a href=\"https:\/\/github.com\/mardix\/active-alchemy\/#create-the-model\">&quot;\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435 \u0410\u043b\u0445\u0438\u043c\u0438\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044f&quot;<\/a> \u0438 <a href=\"https:\/\/github.com\/kofrasa\/flask-activerecord\">\u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a flask-sqlalchemy<\/a> \u043d\u0435 \u0433\u043e\u0434\u044f\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u0427\u0435\u043c \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u044e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b, \u0441\u043c. <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#comparison-with-existing-solutions\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u2014 \u043d\u0438\u0436\u0435 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>  <\/p>\n<p><em>\u041e \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435<\/em><\/p>\n<p>  <\/p>\n<p>\u042f \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u043e\u0433\u043e \u0431\u043b\u043e\u0433\u0430 \u0441 \u0442\u0438\u043f\u043e\u0432\u044b\u043c\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 <code>User<\/code>, <code>Post<\/code>, <code>Comment<\/code>.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0445\u0435\u043c\u0430 \u0411\u0414 \u0438 \u043a\u043e\u0434 ORM<\/b><\/p>\n<div class=\"spoiler_text\">\n<p><img decoding=\"async\" src=\"http:\/\/i.piccy.info\/i9\/48bcc885c5ac01ac036cccfb9c4cbe74\/1489598411\/10896\/1127895\/diagram.png\" alt=\"DB\"\/><\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class User(Base):     __tablename__ = 'user'     id = Column(Integer, primary_key=True)     name = Column(String)      posts = relationship('Post')  class Post(Base):     __tablename__ = 'post'     id = Column(Integer, primary_key=True)     body = Column(String)     user_id = Column(Integer, ForeignKey('user.id'))      user = relationship('User')     comments = relationship('Comment')  class Comment(Base):     __tablename__ = 'comment'     id = Column(Integer, primary_key=True)     body = Column(String)     user_id = Column(Integer, ForeignKey('user.id'))     post_id = Column(Integer, ForeignKey('post.id'))     rating = Column(Integer)      user = relationship('User')     post = relationship('Post')<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421 \u043a\u043e\u0434\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f <a href=\"https:\/\/gist.github.com\/absent1706\/6d2b3ce1ecb47aeb6df6fa09f30819f0\">\u0442\u0443\u0442<\/a>.<br \/>  \u0412 \u043d\u0451\u043c ORM-\u043a\u043b\u0430\u0441\u0441\u044b, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0438\u0441\u0442\u043e\u0439 \u0410\u043b\u0445\u0438\u043c\u0438\u0438 (\u0431\u0435\u0437 \u043c\u043e\u0435\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430) \u0438 \u043d\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. <\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u0430\u043a \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0410\u043b\u0445\u0438\u043c\u0438\u044e:<\/p>\n<p>  <\/p>\n<pre><code>pip install sqlalchemy<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0442\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 \u0444\u0430\u0439\u043b \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435: <\/p>\n<p>  <\/p>\n<pre><code>python \u0444\u0430\u0439\u043b.py<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h2 id=\"active-record\">Active Record<\/h2>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 Active Record ORM, \u044f \u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043b, \u043f\u043e\u0447\u0435\u043c\u0443 \u044f \u0434\u043e\u043b\u0436\u0435\u043d \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u0438\u0441\u0430\u0442\u044c 3 \u0441\u0442\u0440\u043e\u0447\u043a\u0438<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">bob = User(name='Bobby', age=1) session.add(bob) session.flush()<\/code><\/pre>\n<p>  <\/p>\n<p>\u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0439?<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"> bob = User.create(name='Bobby', age=1)<\/code><\/pre>\n<p>  <\/p>\n<p>\u042f \u043f\u043e\u043d\u0438\u043c\u0430\u044e, \u0447\u0442\u043e \u0440\u0443\u0447\u043d\u043e\u0439 flush <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/session_basics.html\">\u0441\u0435\u0441\u0441\u0438\u0438<\/a> \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0411\u0414 \u043f\u043e\u0448\u043b\u0438 \u043e\u0434\u043d\u043e\u0439 \u043f\u0430\u0447\u043a\u043e\u0439, \u0434\u0430 \u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d <a href=\"https:\/\/martinfowler.com\/eaaCatalog\/unitOfWork.html\">unit of work<\/a> \u0434\u0430\u0451\u0442 \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432 \u0432 \u043f\u043b\u0430\u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u2014 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u0439 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/CRUD\">CRUD<\/a>, \u0438 \u043e\u0442\u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 \u0411\u0414 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0435 3 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u043e\u0434\u0438\u043d, \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043d\u0435\u0432\u0435\u043b\u0438\u043a. \u0412\u043e \u0432\u0441\u044f\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043e\u043d \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430. \u0414\u0430 \u0438 \u0432\u043e\u043e\u0431\u0449\u0435, \u043d\u0435 \u0437\u0440\u044f \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u0438 Django, Ruby on Rails, Laravel, Yii \u0432\u044b\u0431\u0440\u0430\u043b\u0438 Active Record ORM.<\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u0436, \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c <strong>Active Record \u043f\u043e\u0432\u0435\u0440\u0445 Data Mapper<\/strong>! \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e-\u0442\u043e \u0438 \u043d\u0430\u0434\u043e, \u0447\u0442\u043e \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 <\/p>\n<p>  <\/p>\n<pre><code class=\"python\">BaseModel.set_session(session) # \u044d\u0442\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u041e\u0420\u041c # \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a BaseModel.session<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u041e\u0420\u041c \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0435\u0441\u0441\u0438\u0438, \u0438 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins\/blob\/master\/sqlalchemy_mixins\/activerecord.py\">\u043c\u0435\u0442\u043e\u0434\u044b save, create, update, delete<\/a> \u0438 \u0442.\u0434.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">bob = User.create(name='Bobby', age=1) bob.update(name='Bob', age=21) bob.delete()<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u0430\u0441\u0447\u0451\u0442 update<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0412\u043e\u043e\u0431\u0449\u0435-\u0442\u043e, \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u043e\u043c \u0441\u043b\u043e\u0435 SQLAlchemy \u0430\u043f\u0434\u0435\u0439\u0442 \u043f\u043e\u043b\u0435\u0439 \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/core\/selectable.html#sqlalchemy.sql.expression.TableClause.update\">\u0435\u0441\u0442\u044c<\/a>, \u043d\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u0438\u043c\u0435\u0442\u044c \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 ORM.<\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041d\u0443 \u0438 \u0435\u0449\u0451 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u044c<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">User.query # \u0432\u043c\u0435\u0441\u0442\u043e session.query(User)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438 \u0431\u044b\u0441\u0442\u0440\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0438\u043b\u0438 \u0432\u0441\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 <\/p>\n<p>  <\/p>\n<pre><code class=\"python\">User.first() # \u0432\u043c\u0435\u0441\u0442\u043e session.query(User).first() User.all() # \u0432\u043c\u0435\u0441\u0442\u043e session.query(User).all()<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438\u043b\u0438 \u043d\u0430\u0439\u0442\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u043e id, \u043e\u0431\u0432\u0430\u043b\u0438\u0432\u0448\u0438\u0441\u044c \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">User.find(1) # \u0432\u043c\u0435\u0441\u0442\u043e session.query(User).get(1) User.find_or_fail(123987) # \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 Active Record \u043a\u0430\u043a \u0432 \u043b\u044e\u0431\u0438\u043c\u044b\u0445 \u043c\u043d\u043e\u044e Django, Laravel \u0438 Ruby on Rails, \u043d\u043e \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0443 \u043d\u0430\u0441 \u043c\u043e\u0449\u043d\u044b\u0439 Data Mapper. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u0438\u043c\u0435\u0435\u043c <em>\u043b\u0443\u0447\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043c\u0438\u0440\u043e\u0432<\/em>.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043c. <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#active-record\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  <\/p>\n<h2 id=\"eager-load\">Eager Load<\/h2>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/tutorial.html?highlight=eager%20load#eager-loading\">\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/a> \u043a\u0430\u0436\u0434\u0430\u044f ORM \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 10 \u044e\u0437\u0435\u0440\u043e\u0432 \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u0442\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044e\u0437\u0435\u0440\u0430. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u044b\u0448\u043b\u043e 11 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (1 \u043d\u0430 \u044e\u0437\u0435\u0440\u043e\u0432 \u0438 10 \u043d\u0430 \u043f\u043e\u0441\u0442\u044b), \u0432 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/loading_relationships.html#using-loader-strategies-lazy-loading-eager-loading\">SQLAlchemy<\/a> \u043c\u043e\u0436\u043d\u043e \u044d\u0442\u0438 \u043f\u043e\u0441\u0442\u044b \u043f\u0440\u0438\u0434\u0436\u043e\u0439\u043d\u0438\u0442\u044c<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session.query(User).options(joinedload('posts'))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438\u043b\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session.query(User).options(subqueryload('posts'))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u0436, \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e! \u0422\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0442 \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0441 \u043f\u043e\u0441\u0442\u0430\u043c\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u0430 \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438 \u0438\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432? \u0410\u043b\u0445\u0438\u043c\u0438\u044f \u044d\u0442\u043e <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/loading_relationships.html#sqlalchemy.orm.joinedload_all\">\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442<\/a>, \u043d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u044d\u0442\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e.<\/p>\n<p>  <\/p>\n<p>\u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u0438\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">User.with_({     'posts': {         'comments': {             'user': None         }     } }.all()<\/code><\/pre>\n<p>  <\/p>\n<p>\u043c\u043e\u0436\u043d\u043e \u0438 \u0431\u0435\u0437 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">User.with_({     User.posts: {         Post.comments: {             Comment.user: None         }     } }.all()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438: <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/loading_relationships.html#sqlalchemy.orm.joinedload\">joinedload<\/a>(\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e) \u0438\u043b\u0438 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/loading_relationships.html#sqlalchemy.orm.subqueryload\">subqueryload<\/a>.<br \/>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a 2 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c: \u043d\u0430 \u043f\u043e\u0441\u0442\u044b \u0438 \u043d\u0430 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0441 \u043f\u0440\u0438\u0434\u0436\u043e\u0439\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u044e\u0437\u0435\u0440\u0430\u043c\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">Post.with_({     'comments': (SUBQUERYLOAD, {  # \u0433\u0440\u0443\u0437\u0438\u043c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c          'user': None  # \u0438 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044e \u0434\u0436\u043e\u0439\u043d\u0438\u043c \u0430\u0432\u0442\u043e\u0440\u0430      }) }}<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#eager-load\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  <\/p>\n<p><strong>P.S.<\/strong> \u041e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u043c\u043e\u0438\u043c \u043a\u043e\u043b\u043b\u0435\u0433\u0430\u043c \u0437\u0430 \u043a\u043e\u0434. <\/p>\n<p>  <\/p>\n<h2 id=\"magicheskie-operatory-i-join-otnosheniy-kak-v-django\">\u041c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438 join \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439, \u043a\u0430\u043a \u0432 Django<\/h2>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043c\u043d\u0435 \u0431\u0440\u043e\u0441\u0438\u043b\u043e\u0441\u044c \u0432 \u0433\u043b\u0430\u0437\u0430 \u043f\u0440\u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0438 Django \u2014 \u044d\u0442\u043e <a href=\"https:\/\/docs.djangoproject.com\/en\/1.10\/topics\/db\/queries\/#retrieving-specific-objects-with-filters\">\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0445<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">Entry.objects.filter(headline__startswith=&quot;What&quot;)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438 \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u043e\u0440\u0430\u0437\u0438\u043b\u0430 <a href=\"https:\/\/docs.djangoproject.com\/en\/1.10\/topics\/db\/queries\/#lookups-that-span-relationships\">\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e \u0441\u0432\u044f\u0437\u044f\u043c<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">Entry.objects.filter(blog__name='Beatles Blog')<\/code><\/pre>\n<p>  <\/p>\n<p>\u044d\u0442\u043e \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c \u0431\u043e\u043b\u0435\u0435 &quot;\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435&quot; \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u0410\u043b\u0445\u0438\u043c\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session.query(Entry).join(Entry.blog).filter(Blog.name=='Beatles Blog')<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0425\u043e\u0442\u044f&#8230;<\/b><\/p>\n<div class=\"spoiler_text\">\n<ul>\n<li>\u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043c\u0435\u043d\u0435\u0435 \u0433\u0438\u0431\u043a\u043e, \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0442\u0443\u0442 \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u0442<\/li>\n<li>\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u0430\u0442\u044c \u0431\u0430\u0433 \u0432 Runtime, \u0435\u0441\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u043f\u0435\u0447\u0430\u0442\u043a\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043c\u0435\u0441\u0442\u043e <code>blog__name<\/code> \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>blogg__name<\/code>. \u0422\u0430\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u0440\u043e\u0434\u0435 <code>Entry.blog<\/code>, IDE \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0441\u043f\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c. <\/li>\n<\/ul>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0441\u0442\u0435\u0442\u0438\u043a\u0438, \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u044b \u0441 UI):<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">filters =  {'entry__headline__contains': 'Lennon', 'entry__pub_date__year': 2008} # \u044d\u0442\u043e \u043c\u043e\u0433 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434 Blog.objects.filter(**filters)<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445, \u0433\u0434\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u044b. <\/p>\n<p>  <\/p>\n<p>\u0423\u0432\u044b, \u0432 \u0410\u043b\u0445\u0438\u043c\u0438\u0438 \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441\u0442\u043e\u043b\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u043d\u043e. \u041c\u0430\u043a\u0441\u0438\u043c\u0443\u043c, \u0447\u0442\u043e \u043e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u2014 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/query.html#sqlalchemy.orm.query.Query.filter_by\">\u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e<\/a> \u0442\u0438\u043f\u0430 &quot;\u043a\u043e\u043b\u043e\u043d\u043a\u0430=\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435&quot;:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session.query(MyClass).filter_by(name = 'some name')<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0437\u044f\u0432 \u0437\u0430 \u043e\u0431\u0440\u0430\u0437\u0435\u0446 <a href=\"https:\/\/github.com\/mitsuhiko\/sqlalchemy-django-query\">\u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/a> (\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0441\u0451 \u0436\u0435 \u0431\u044b\u043b\u043e <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#django-like-queries-1\">\u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e<\/a>), \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0441\u0432\u043e\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 \u0414\u0436\u0430\u043d\u0433\u0438 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">Post.where(rating__in=[2, 3, 4], user___name__like='%Bi%').all()<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0421\u0442\u0440\u043e\u043a\u0430 <code>user___name__like<\/code> \u043f\u0430\u0440\u0441\u0438\u0442\u0441\u044f \u0438 \u043c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u043f\u0440\u0438\u0434\u0436\u043e\u0439\u043d\u0438\u0442\u044c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 <code>Post.user<\/code> \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440 <code>User.name.like('...')<\/code>.<br \/>  \u0422\u043e \u0435\u0441\u0442\u044c <\/p>\n<p>  <\/p>\n<pre><code class=\"python\">Post.where(user___name__like='%Bi%').all()<\/code><\/pre>\n<p>  <\/p>\n<p>\u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session.query(Post).join(Post.user).filter(User.name.like('%Bi%')).all()<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0410 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u0441\u043b\u043e\u0436\u043d\u0435\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0412\u043e\u043e\u0431\u0449\u0435-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0442\u0430\u043a, \u0447\u0442\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 2 \u0440\u0430\u0437\u0430.<br \/>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u044f \u0445\u043e\u0447\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u044e\u0437\u0435\u0440\u043e\u0432, \u043f\u043e\u0441\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u0412\u0430\u0441\u044f <\/p>\n<p>  <\/p>\n<pre><code class=\"python\">User.where(posts___comments___user___name='Vasya').all()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u0442\u044c \u044e\u0437\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044e, \u0430 \u0435\u0441\u0442\u044c \u0430\u0432\u0442\u043e\u0440 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f.<br \/>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0440\u0435\u0448\u0430\u044e\u0442 \u0447\u0435\u0440\u0435\u0437 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/query.html#sqlalchemy.orm.aliased\">alias&#8217;\u044b<\/a>, \u0442.\u0435. \u0432 <a href=\"http:\/\/www.qopy.me\/4KOkgXYKTOiXHUbc4R6EnQ\">\u0438\u0442\u043e\u0433\u043e\u0432\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/a> \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c 2 \u0442\u0430\u0431\u043b\u0438\u0446\u044b: <code>user<\/code> \u0438 <code>user_1<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u043d\u0430\u0442\u044c, \u0431\u0443\u0434\u0443\u0442 \u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c\u0441\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0435\u043b\u0430\u0435\u043c \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u0436\u043e\u0439\u043d\u0438\u043c, \u0441\u0432\u043e\u0439 alias:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">post_alias = User.posts.property.argument() # \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0438\u0437 relationship session.query(User).outerjoin(post_alias) # \u0438 \u0442.\u0434.<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins\/blob\/master\/sqlalchemy_mixins\/smartquery.py\">\u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from sqlalchemy.orm import aliased from sqlalchemy.sql import operators  # \u0418\u043c\u0435\u0435\u043c \u043d\u0430 \u0432\u0445\u043e\u0434\u0435 {'posts___comments___user___name__like': 'Vasya'}. \u0414\u043e\u0441\u0442\u0430\u043d\u0435\u043c: relations = ['posts', 'comments', 'user'] # 1. \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f, \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b ___ attr_name = 'name' # 2. \u0430\u0442\u0442\u0440\u0438\u0431\u0443\u0442, \u043e\u043d \u0431\u044b\u043b \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e ___ op_name = 'like' # 3. \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043e\u043d \u0431\u044b\u043b \u043f\u043e\u0441\u043b\u0435 __ # \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0410\u043b\u0445\u0438\u043c\u0438\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 op_name. # \u0432 \u0440\u0435\u0430\u043b\u0435 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 OPERATORS = {'like': operators.like_op}, # \u0438 \u0438\u0437 \u043d\u0435\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043a\u0430\u043a OPERATORS[op_name] operator = operators.like_op  value = 'Vasya'  cls = User # \u0432 \u0436\u0438\u0437\u043d\u0438 \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 cls query = session.query(cls) # \u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441  # \u0434\u0436\u043e\u0439\u043d\u0438\u043c \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 last_alias = cls for relation in relations:     relation = getattr(last_alias, relation) # \u0431\u0435\u0440\u0451\u043c relation, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 User.posts     next_alias = aliased(relation.property.argument()) # \u0434\u043e\u0441\u0442\u0430\u0451\u043c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 (Post \u0434\u043b\u044f User.posts)                                                         # \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430 \u043d\u0435\u0433\u043e alias     query = query.outerjoin(next_alias) # \u0434\u0436\u043e\u0439\u043d\u0438\u043c \u043a\u043b\u0430\u0441\u0441\/\u0430\u043b\u0438\u0430\u0441     last_alias = next_alias  # \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c SQL-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443\/\u0430\u043b\u0438\u0430\u0441\u0443 \u0432 \u0446\u0435\u043f\u043e\u0447\u043a\u0435 (\u0443 \u043d\u0430\u0441 \u044d\u0442\u043e User) attr = getattr(last_alias, attr_name) # \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u0442\u0442\u0440\u0438\u0431\u0443\u0442 User.name query = query.filter(operator(attr, value)) # \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c SQL-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u0435\u043c\u0443 \u0430\u0442\u0442\u0440\u0438\u0431\u0443\u0442 User.name \u0438 \u0412\u0430\u0441\u044e print(query.all())<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 <a href=\"https:\/\/gist.github.com\/absent1706\/6d2b3ce1ecb47aeb6df6fa09f30819f0\">\u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a \u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u043a\u043e\u0434<\/a>. <\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">Post.sort('-rating', 'user___name').all() # sort by rating DESC, user name ASC<\/code><\/pre>\n<p>  <\/p>\n<h4 id=\"avtomaticheskiy-eager-load\">\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 eager load<\/h4>\n<p>  <\/p>\n<p>\u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0440\u0430\u0437 \u0443\u0436 \u043c\u044b \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0435\u043b\u0430\u0435\u043c join \u0441\u0432\u044f\u0437\u0435\u0439, \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c SQLAlchemy, \u0447\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u0438 \u0443\u0436\u0435 \u043f\u0440\u0438\u0434\u0436\u043e\u0439\u043d\u0435\u043d\u044b, \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <a href=\"http:\/\/docs.sqlalchemy.org\/en\/latest\/orm\/loading_relationships.html#sqlalchemy.orm.contains_eager\">contains_eager<\/a>. \u0422\u0435\u043f\u0435\u0440\u044c, \u0435\u0441\u043b\u0438 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 <code>Post.user<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0435\/\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435, \u0442\u043e \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u044e\u0437\u0435\u0440\u0430 <strong>\u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/strong>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">post = Post.sort('user___name').first() print(post.user) # \u043d\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0411\u0414, \u0442.\u043a. \u044e\u0437\u0435\u0440 \u0431\u044b\u043b \u043f\u0440\u0438\u0434\u0436\u043e\u0439\u043d\u0435\u043d<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0441\u043c. <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#filter-and-sort-by-relations\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  <\/p>\n<h2 id=\"vsyo-v-kuchu-filtry-sortirovka-eager-load\">\u0412\u0441\u0451 \u0432 \u043a\u0443\u0447\u0443: \u0444\u0438\u043b\u044c\u0442\u0440\u044b, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, eager load<\/h2>\n<p>  <\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0438\u0440\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0434\u0430 \u0435\u0449\u0451 \u0438 eager load&#8217;\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0438.<\/p>\n<p>  <\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0441\u0442\u044b \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438 \u0442\u043e\u043c\u0443 \u0436\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e <code>Post.user<\/code>. \u041c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 2 \u0440\u0430\u0437\u0430 \u043f\u0440\u0438\u0434\u0436\u043e\u0439\u043d\u044f\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0441\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0420\u0430\u0437\u0432\u0435 \u0410\u043b\u0445\u0438\u043c\u0438\u044f \u0441\u0430\u043c\u0430 \u043d\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u0442\u0441\u044f?<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">session.query(Post).join(Post.user).join(Post.user)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0442\u043e, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0410\u043b\u0445\u0438\u043c\u0438\u044f \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d join.<br \/>  \u0428\u0442\u0443\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u0432\u043e\u0439 alias (\u0441\u043c. \u0441\u043f\u043e\u0439\u043b\u0435\u0440 &quot;<em>\u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e<\/em>&quot; \u0432\u044b\u0448\u0435), \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0410\u043b\u0445\u0438\u043c\u0438\u044f \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e 2 alias-\u0430 \u043d\u0430 <code>Post.user<\/code> \u2014 \u044d\u0442\u043e \u043f\u043e \u0441\u0443\u0442\u0438 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u0438 \u043d\u0430\u0434\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u044d\u0442\u0438\u043c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e. <\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0438 eager load (\u0434\u0430, \u0435\u0433\u043e \u0442\u043e\u0436\u0435) \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0432\u0441\u0435\u0445 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0445 \u0434\u0436\u043e\u0438\u043d\u0430\u0445 (\u0442\u043e\u0447\u043d\u0435\u0435, \u0438\u043c\u0435\u0442\u044c \u0435\u0434\u0438\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a alias-\u043e\u0432, \u0441\u043c. \u0441\u043f\u043e\u0439\u043b\u0435\u0440 &quot;<em>\u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e<\/em>&quot;) \u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0445 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"> Comment.smart_query(      filters={          'post___public': True,          'user__isnull': False      },      sort_attrs=['user___name', '-created_at'],      schema={          'post': {              'user': None          }      }).all()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#all-in-one-smart_query\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  <\/p>\n<h1 id=\"udobnaya-otladka-s-udobnym-__repr__\">\u0423\u0434\u043e\u0431\u043d\u0430\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u0441 \u0443\u0434\u043e\u0431\u043d\u044b\u043c <code>__repr__<\/code><\/h1>\n<p>  <\/p>\n<p>\u041c\u043d\u0435, \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443, \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438. \u0418 \u0431\u044b\u043b\u043e \u043e\u0447\u0435\u043d\u044c \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e \u043f\u043e\u0441\u043b\u0435 <a href=\"https:\/\/cdn.scotch.io\/16\/HB9eyVdFRKyjdnxV63Yp_Capture.PNG\">\u043a\u0440\u0430\u0441\u043e\u0442\u044b<\/a> \u0432 <a href=\"https:\/\/scotch.io\/tutorials\/tinker-with-the-data-in-your-laravel-apps-with-php-artisan-tinker#creating-a-new-user\">Laravel<\/a> \u0432\u0438\u0434\u0435\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0435:<\/p>\n<p>  <\/p>\n<pre><code>&gt;&gt;&gt; session.query(Post).all() [&lt;myapp.models.Post object at 0x04287A50&gt;, &lt;myapp.models.Post object at 0x04287A90&gt;]<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0436\u0443\u0442\u043a\u043e \u043d\u0435\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u043e <del>\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u0435\u0441\u0438\u0442<\/del>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0445\u043e\u0442\u044f \u0431\u044b ID <\/p>\n<p>  <\/p>\n<pre><code>&gt;&gt;&gt; session.query(Post).all() [&lt;Post #11&gt;, &lt;Post #12&gt;]<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u0430\u043a \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c ID<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">from sqlalchemy import inspect ids = inspect(post).identity # \u0432\u0435\u0440\u043d\u0451\u0442 \u043a\u043e\u0440\u0442\u0435\u0436 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 primary \u043a\u043b\u044e\u0447\u0435\u0439<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0410 \u0432\u043e\u043e\u0431\u0449\u0435 \u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0435\u0441\u0442\u044c 1-2 \u0430\u0442\u0442\u0440\u0438\u0431\u0443\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u0435\u0451 \u0441\u0443\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0443 \u044e\u0437\u0435\u0440\u0430 \u044d\u0442\u043e \u0438\u043c\u044f, \u0430 \u0443 \u043f\u043e\u0441\u0442\u0430 \u044d\u0442\u043e \u044e\u0437\u0435\u0440 \u0438 \u0442\u0435\u043b\u043e \u043f\u043e\u0441\u0442\u0430. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0442\u044c \u044d\u0442\u0438 \u0430\u0442\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class User(BaseModel):     __repr_attrs__ = ['name']     # ...  class Post(BaseModel):     __repr_attrs__ = ['user', 'body'] # body is just column, user is relationship     # ... <\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0438\u043c\u0435\u0435\u043c \u0441\u043e\u0432\u0441\u0435\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 <code>__repr__<\/code>:<\/p>\n<p>  <\/p>\n<pre><code>&gt;&gt;&gt; session.query(Post).all() [&lt;Post #11 user:&lt;User #1 'Bill'&gt; body:'post 11'&gt;,  &lt;Post #12 user:&lt;User #2 'Bob'&gt; body:'post 12'&gt;] <\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins#beauty-__repr__\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  <\/p>\n<h1 id=\"itogi\">\u0418\u0442\u043e\u0433\u0438<\/h1>\n<p>  <\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0444\u0438\u0447\u0438, \u0431\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043b\u0438\u0447\u043d\u043e \u043c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0442\u044f\u0436\u043a\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e\u0441\u044c \u0441 \u0410\u043b\u0445\u0438\u043c\u0438\u0435\u0439.<br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u0436\u0438\u0442\u044c \u0441\u0442\u0430\u043b\u043e \u043b\u0435\u0433\u0447\u0435, \u0432\u0435\u0434\u044c: <\/p>\n<p>  <\/p>\n<ol>\n<li>C Active Record \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u043e \u0434\u0435\u043b\u0430\u0442\u044c CRUD, \u043d\u0435 \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044f\u0441\u044c \u0441 flush-\u0435\u043c \u0441\u0435\u0441\u0441\u0438\u0438.<\/li>\n<li>\u0421 \u043c\u043e\u0449\u043d\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0435\u0439\/\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0432 \u0441\u0442\u0438\u043b\u0435 Django \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435, <em>\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435<\/em> \u0434\u0436\u043e\u0439\u043d\u044b \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0445\u043e\u0442\u044c \u0441 UI \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c.<\/li>\n<li>\u041c\u044b \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c eager load \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e.<\/li>\n<li>\u041e\u0442\u043b\u0430\u0434\u043a\u0430 \u0441\u0442\u0430\u043b\u0430 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u044b\u043c <code>__repr__<\/code>.<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u041d\u0430 \u0431\u043e\u0435\u0432\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u043b\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430.<br \/>  \u0412\u0441\u0435 \u0444\u0438\u0447\u0438, \u043e\u043f\u044f\u0442\u044c \u0436\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b <a href=\"https:\/\/github.com\/absent1706\/sqlalchemy-mixins\">\u0442\u0443\u0442<\/a>.<\/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> \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:\/\/habrahabr.ru\/post\/324876\/\"> https:\/\/habrahabr.ru\/post\/324876\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e <a href=\"https:\/\/www.sqlalchemy.org\/\">SQLAlchemy<\/a> \u2014 \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f ORM \u043d\u0430 Python. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u0432\u0435\u0449\u0438, \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e Active Record \u0441\u043e\u0431\u0440\u0430\u0442\u044c\u0435\u0432. \u041d\u043e \u043f\u043b\u0430\u0442\u0430 \u0437\u0430 \u044d\u0442\u043e \u2014 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043a\u043e\u0434, \u0438 \u0432 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0432\u0440\u043e\u0434\u0435 CRUD \u044d\u0442\u043e \u043d\u0430\u043f\u0440\u044f\u0433\u0430\u0435\u0442.<\/p>\n<p>  <\/p>\n<p>\u041e \u0442\u043e\u043c, \u043a\u0430\u043a \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0410\u043b\u0445\u0438\u043c\u0438\u044e \u0443\u0434\u043e\u0431\u043d\u043e\u0439, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0432\u0448\u0438\u0441\u044c \u043e\u043f\u044b\u0442\u043e\u043c \u043b\u0443\u0447\u0448\u0438\u0445 Active Record ORM, \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/p>\n<p>  <\/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-283921","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/283921","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=283921"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/283921\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}