{"id":357120,"date":"2024-05-20T23:43:56","date_gmt":"2024-05-20T23:43:56","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=357120"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=357120","title":{"rendered":"<span>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 ORM \u0434\u043b\u044f sqlite3<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><a href=\"https:\/\/habr.com\/ru\/company\/ruvds\/blog\/766552\/\"><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/wn\/sj\/kg\/wnsjkgsyk9aexkxg04zlrar07zo.png\" data-src=\"https:\/\/habrastorage.org\/webt\/wn\/sj\/kg\/wnsjkgsyk9aexkxg04zlrar07zo.png\"\/><\/div>\n<p><\/a><br \/>  ORM, \u0438\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e-\u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u044b. \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u0438\u0441\u0430\u0442\u044c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ORM, \u0447\u0442\u043e\u0431\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435.<\/p>\n<p>  \u041d\u0435 \u0431\u044b\u0432\u0430\u043b\u043e \u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438\u0437\u043d\u0443\u0442\u0440\u0438 \u0442\u0430\u043a\u0430\u044f \u0438\u0434\u0435\u0439\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f? \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0440\u0430\u0431\u043e\u0442\u044b? \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c ORM \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b python \u043d\u0430\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f.<a name=\"habracut\"><\/a><\/p>\n<p>  \u0420\u0435\u043c\u0430\u0440\u043a\u0430: sqlite3 \u0432\u044b\u0431\u0440\u0430\u043d \u0438\u0437-\u0437\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b, \u043d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u043d\u0435\u043c\u0443 \u043d\u0430 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u043b\u044e\u0431\u043e\u0439 \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0434\u043b\u044f \u0432\u0430\u0441 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0443 \u044f \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u043d\u0430 \u0434\u0436\u0430\u043d\u0433\u043e.<\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/font><\/h2>\n<p>  \u0412 sqlite3 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442: INTEGER \u2014 \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e, TEXT \u2014 \u0442\u0435\u043a\u0441\u0442, BLOB \u2014 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, REAL \u2014 \u0447\u0438\u0441\u043b\u043e \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439(float24), NUMERIC \u2014 \u0442\u043e \u0436\u0435, \u0447\u0442\u043e \u0438 INTEGER.<\/p>\n<pre><code class=\"sql\">CREATE TABLE \"example\" (     \"Field1\"    INTEGER NOT NULL,     \"Field2\"    TEXT UNIQUE,     \"Field3\"    BLOB,     \"Field4\"    REAL DEFAULT 123,     \"Field5\"    NUMERIC ); <\/code><\/pre>\n<p>  \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b NULL, UNIQUE, DEFAULT, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">class BaseType:   field_type: str #\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \"INTEGER\"    def __init__(self, unique: bool = False, null: bool = True, default: int = None):       self.unique = unique       self.null = null       self.default = default<\/code><\/pre>\n<p>  \u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043d\u0435\u0433\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b:<\/p>\n<pre><code class=\"python\">class IntegerField(BaseType):   field_type = 'INTEGER'   class TextField(BaseType):   field_type = 'TEXT'   class BlobField(BaseType):   field_type = 'BLOB'   class RealField(BaseType):   field_type = 'REAL'   class NumericField(BaseType):   field_type = 'NUMERIC' <\/code><\/pre>\n<p>  <\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438<\/font><\/h2>\n<p>  \u042f \u0445\u043e\u0447\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">class Box(models.Model):   name = TextField()   width = IntegerField()   height = IntegerField()<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 Model. \u041e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0438\u0448\u0435\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">class Model:   def __init__(self, *args, **kwargs):       fields = [el for el in vars(self.__class__) if not el.startswith(\"__\")] #\u043f\u043e\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 name, width, height)       for i, value in enumerate(args):           setattr(self, fields[i], value)#\u0437\u0430\u0434\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e args        for field, value in kwargs.items():#\u0437\u0430\u0434\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e kwargs           setattr(self, field, value)<\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 Model \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043b\u044e\u0431\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0437\u0430\u0434\u0430\u0434\u0438\u043c. <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u043e args \u0438 kwargs<\/b>                         <\/p>\n<div class=\"spoiler_text\">1. *args:<br \/>   \u2014 *args \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<br \/>   \u2014 \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u0430\u043a *args, \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u0442\u0435\u0436 (tuple) \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<br \/>   \u2014 \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u0438\u043c\u044f \u0432\u043c\u0435\u0441\u0442\u043e \u00abargs\u00bb, \u043d\u043e \u043e\u0431\u0449\u0435\u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u00abargs\u00bb.<br \/>  \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">def print_args(*args): for arg in args:     print(arg)  print_args(1, 2, 3)  # \u0412\u044b\u0432\u043e\u0434\u0438\u0442: 1, 2, 3 <\/code><\/pre>\n<p>  2. **kwargs:<br \/>   \u2014 **kwargs \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 (\u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435) \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<br \/>   \u2014 \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u0430\u043a **kwargs, \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c (dictionary) \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<br \/>   \u2014 \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u0438\u043c\u044f \u0432\u043c\u0435\u0441\u0442\u043e \u00abkwargs\u00bb, \u043d\u043e \u043e\u0431\u0449\u0435\u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u00abkwargs\u00bb.<br \/>  \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">def print_kwargs(**kwargs): for key, value in kwargs.items():     print(f\"{key}: {value}\")  print_kwargs(name=\"John\", age=30, city=\"New York\")  # \u0412\u044b\u0432\u043e\u0434\u0438\u0442: name: John, age: 30, city: New York<\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f *args \u0438 **kwargs, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043d\u0430\u0431\u043e\u0440\u044b \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432.  <\/div>\n<\/p><\/div>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0440\u0430\u0437\u0443 \u043c\u0435\u0442\u043e\u0434 json(), \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u043e\u043d \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043d\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f api \u0438\u043b\u0438 \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c. <\/p>\n<pre><code class=\"python\">def json(self):   attributes = {}   for key, value in vars(self).items():       if not key.startswith(\"__\") and not callable(value):#\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u043f\u043e\u043b\u044f           attributes[key] = value    return attributes<\/code><\/pre>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u044b \u0443\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438, \u043d\u043e \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u0431\u0435\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">class Box(models.Model):   name = TextField()   width = IntegerField()   height = IntegerField()   print(Box('Box 1', 1, 1).json())#\u0432\u044b\u0432\u0435\u0434\u0435\u0442 {'name': 'Box 1', 'width': 1, 'height': 1} <\/code><\/pre>\n<p>  <\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c sqlite3<\/font><\/h2>\n<p>  \u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u044f \u0445\u043e\u0447\u0443, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043c\u0435\u043b \u043f\u043e\u043b\u0435 objects \u2014 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a sqlite.<br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">Box.objects.add(Box('BOX 1', 1, 1)) Box.objects.get(name='BOX 1') Box.objects.filter(width=1, height=1) Box.objects.delete(name=\"BOX 1\")<\/code><\/pre>\n<p>  \u041c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043c\u0435\u043b \u044d\u0442\u043e \u043f\u043e\u043b\u0435, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u043b\u043e\u0441\u044c \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u043a\u0441\u0438-\u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440. <\/p>\n<pre><code class=\"python\">class ProxyObjects:   def __get__(self, instance, owner):       return Object(owner)<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u043e __get__<\/b>                         <\/p>\n<div class=\"spoiler_text\">__get__ \u2014 \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0432 Python, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u043e\u0432 \u0432 Python \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0432\u043e\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c.<\/p>\n<p>  __get__ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b __set__ \u0438 __delete__, \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f __get__:<\/p>\n<pre><code class=\"python\">class MyDescriptor: def __get__(self, instance, owner):     if instance is None:         # \u0415\u0441\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u0443 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043a\u043b\u0430\u0441\u0441, \u0430 \u043d\u0435 \u0447\u0435\u0440\u0435\u0437 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440,         # \u0442\u043e instance \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d None, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0430\u043c \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440 \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.         return self     else:         # \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 instance - \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, owner - \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442.         # \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0438\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043d\u0435\u043c\u0443.         return instance._value  class MyClass: def __init__(self, value):     self._value = value  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440 MyDescriptor \u0434\u043b\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 'my_attribute' my_attribute = MyDescriptor()  # \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 obj = MyClass(42)  # \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 'my_attribute' \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 __get__ \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u0430 MyDescriptor print(obj.my_attribute)  # \u0412\u044b\u0432\u0435\u0434\u0435\u0442: 42<\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 MyDescriptor \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 my_attribute \u043a\u043b\u0430\u0441\u0441\u0430 MyClass. \u041c\u0435\u0442\u043e\u0434 __get__ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 obj.  <\/div>\n<\/p><\/div>\n<p>  \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0438\u0448\u0435\u043c \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"python\">import sqlite3  # \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 SQLite  class Object: def __init__(self, object_type: type):     # \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u043a\u043b\u0430\u0441\u0441) \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0435\u0433\u043e \u0432 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0435 object_type.     self.object_type = object_type  def __createTable__(self):     # \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043d\u0430 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u0445 \u043a\u043b\u0430\u0441\u0441\u0430 object_type.      # \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445     conn = sqlite3.connect(db_name)     cursor = conn.cursor()      # \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043f\u0438\u0441\u043e\u043a custom_fields \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.     custom_fields = []      # \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u043a\u043b\u0430\u0441\u0441\u0430 object_type \u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044f\u0445.     for key, value in vars(self.object_type).items():         if not key.startswith(\"__\") and not callable(value):             field_name = key             field_type = value.field_type             is_unique = value.unique             is_null = value.null             default_value = value.default              # \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043b\u044f \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0451 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a custom_fields.             field_declaration = [f'\"{field_name}\" {field_type}']              if is_unique:                 field_declaration.append('UNIQUE')             if not is_null:                 field_declaration.append('NOT NULL')             if default_value is not None:                 field_declaration.append(f'DEFAULT {default_value}')              custom_fields.append(' '.join(field_declaration))      # \u0421\u043e\u0437\u0434\u0430\u0451\u043c SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044f\u043c\u0438.     create_table_sql = f'''     CREATE TABLE IF NOT EXISTS {self.object_type.__name__} (         {\", \".join(custom_fields)}     );     '''      # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c SQL-\u0437\u0430\u043f\u0440\u043e\u0441.     cursor.execute(create_table_sql)      # \u0424\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.     conn.commit()     conn.close()<\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u2014 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"python\">def simple_orm(class_: type):   EXTERN_TYPES[class_.__name__] = class_ # \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043c\u043e\u0434\u0435\u043b\u0435\u0439   class_.objects.__createTable__() # \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0431\u0434    return class_ <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b<\/b>                         <\/p>\n<div class=\"spoiler_text\">\u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u0432 Python \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0434\u0440\u0443\u0433\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043a \u0435\u0451 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u043b\u0438 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0443\u043a\u0440\u0430\u0448\u0435\u043d\u0438\u0435, \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u044f \u0441\u0430\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0438\u043b\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u043b\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043d\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u044f \u0438\u0445 \u043a\u043e\u0434. \u041e\u043d\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043c\u043e\u0449\u043d\u044b\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 Python.<\/p>\n<p>  \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 &#171;@&#187; \u043f\u0435\u0440\u0435\u0434 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u043d\u0438 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u0443\u044e\u0442. \u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430:<\/p>\n<pre><code class=\"python\">def my_decorator(func): def wrapper():     print(\"\u0427\u0442\u043e-\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438\")     func()     print(\"\u0427\u0442\u043e-\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\") return wrapper  @my_decorator def say_hello(): print(\"\u041f\u0440\u0438\u0432\u0435\u0442, \u043c\u0438\u0440!\")  say_hello()<\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 my_decorator \u2014 \u044d\u0442\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 say_hello. \u0417\u0430\u0442\u0435\u043c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 @my_decorator \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 say_hello, \u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 say_hello() \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435, \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c.<\/p>\n<p>  \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u0434\u0430\u0447:<\/p>\n<ol>\n<li>\u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: \u0417\u0430\u043f\u0438\u0441\u044c \u043b\u043e\u0433\u043e\u0432 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u043b\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432.<\/li>\n<li>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438: \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u0440\u0430\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/li>\n<li>\u041a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0441 \u0442\u0435\u043c\u0438 \u0436\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438.<\/li>\n<li>\u0418\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f: \u041e\u0446\u0435\u043d\u043a\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/li>\n<li>\u041c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f: \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/li>\n<\/ol>\n<p>  Python \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a @staticmethod, @classmethod, @property \u0438 \u0434\u0440\u0443\u0433\u0438\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0432\u0430\u0448\u0438\u043c\u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044f\u043c\u0438.  <\/div>\n<\/p><\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432 \u0431\u0434 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043f\u043e\u043b\u044f\u043c\u0438:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/3ce\/fe5\/fa5\/3cefe5fa57202eacc273bba4ee1537c1.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/3ce\/fe5\/fa5\/3cefe5fa57202eacc273bba4ee1537c1.png\"\/><\/p>\n<h2><font color=\"#3AC1EF\">\u258d JsonField<\/font><\/h2>\n<p>  \u0412 sqlite3 \u043d\u0435\u0442 JsonField \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u043a\u0441\u0442\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b:<\/p>\n<pre><code class=\"python\">class JsonField(BaseType):   field_type = 'JSON'<\/code><\/pre>\n<p>  Json, \u043f\u043e \u0441\u0443\u0442\u0438, \u0432\u0435\u0434\u0451\u0442 \u0441\u0435\u0431\u044f, \u043a\u0430\u043a \u0442\u0435\u043a\u0441\u0442, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c json.dumps(), \u0430 \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u2014 json.loads().<\/p>\n<h2><font color=\"#3AC1EF\">\u258d ForeignKey<\/font><\/h2>\n<p>  \u0412 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0431\u0430\u0437\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u00abforeign key\u00bb (\u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447) \u2014 \u044d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438. \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0434\u0438\u043d \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u043e\u043c (\u043e\u0431\u044b\u0447\u043d\u043e) \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u042d\u0442\u0430 \u0441\u0432\u044f\u0437\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445.<\/p>\n<p>  ForeignKey \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f.<\/p>\n<pre><code class=\"python\">class ForeignKey(BaseType):   field_type = 'FOREIGN_KEY'    def __init__(self, object_class: type, foreign_field: str, unique: bool = False,                 null: bool = True, default=None):       self.object_class = object_class,       self.foreign_field = foreign_field,       self.unique = unique       self.null = null       self.default = default<\/code><\/pre>\n<p>  \u042f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e \u0435\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e, \u043a\u0430\u043a json \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 type, key, value, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"json\">{\"type\": \"Box\", \"key\": \"name\", \"value\": \"BOX 1\"}.<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">@simple_orm class Box(models.Model):   name = TextField()   width = IntegerField()   height = IntegerField()   @simple_orm class Circle(models.Model):   box = ForeignKey(object_class=Box, foreign_field='name')   name = TextField()   radius = IntegerField()   data = JsonField()   box = Box.objects.add(Box('BOX 1', 1, 1)) circle = Circle.objects.add(Circle(box, \"CIRCLE 1\", 5, {'data': 5})) print(circle.json()) #{'box': &lt;__main__.Box object at 0x7f7637f6d850>, 'name': 'CIRCLE 1', 'radius': 5, 'data': {'data': 5}}<\/code><\/pre>\n<p>  <\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c CRUD<\/font><\/h2>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 Object<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">class Object:   def __init__(self, object_type):       self.object_type = object_type    def add(self, obj):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()        d = copy.copy(obj.__dict__)        object_type_name = self.object_type.__name__       for key, value in vars(self.object_type).items():           if not key.startswith(\"__\") and not callable(value):               if type(value) in BASIC_TYPES:                   continue               if type(value) == JsonField:                   d[key] = json.dumps(d[key])               if type(value) == ForeignKey:                   d[key] = json.dumps({'type': value.object_class[0].__name__, 'key': value.foreign_field[0], 'value': getattr(d[key], value.foreign_field[0])})        insert_sql = f'INSERT INTO {object_type_name} ({\", \".join(obj.__dict__.keys())}) VALUES ({\", \".join([\"?\"] * len(obj.__dict__))});'        values = tuple(d.values())       cursor.execute(insert_sql, values)       conn.commit()       conn.close()       return obj    def save(self, obj):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()        d = copy.copy(obj.__dict__)        object_type_name = self.object_type.__name__       for key, value in vars(self.object_type).items():           if not key.startswith(\"__\") and not callable(value):               if type(value) in BASIC_TYPES:                   continue               if type(value) == JsonField:                   d[key] = json.dumps(d[key])               if type(value) == ForeignKey:                   d[key] = json.dumps({'type': value.object_class[0].__name__, 'key': value.foreign_field[0], 'value': getattr(d[key], value.foreign_field[0])})        upsert_sql = f'INSERT OR REPLACE INTO {object_type_name} ({\", \".join(obj.__dict__.keys())}) VALUES ({\", \".join([\"?\"] * len(obj.__dict__))});'        values = tuple(d.values())       cursor.execute(upsert_sql, values)       conn.commit()       conn.close()       return obj    def get(self, **kwargs):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()        object_type_name = self.object_type.__name__        attr_value_pairs = [(attr, value) for attr, value in kwargs.items()]        where_clauses = [f'{attr} = ?' for attr, _ in attr_value_pairs]       where_clause = ' AND '.join(where_clauses)       select_by_attrs_sql = f'SELECT * FROM {object_type_name} WHERE {where_clause};'        values = tuple(value for _, value in attr_value_pairs)        cursor.execute(select_by_attrs_sql, values)       row = cursor.fetchone()       conn.close()        if row:           obj = self.object_type()           for i, value in enumerate(row):               if type(getattr(obj, cursor.description[i][0])) == JsonField:                   setattr(obj, cursor.description[i][0], json.loads(value))               elif type(getattr(obj, cursor.description[i][0])) == ForeignKey:                   value = json.loads(value)                   setattr(obj, cursor.description[i][0], EXTERN_TYPES[value['type']].objects.get(**{value['key']: value['value']}))               else:                   setattr(obj, cursor.description[i][0], value)            return obj       else:           return None    def delete(self, **kwargs):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()        object_type_name = self.object_type.__name__        attr_value_pairs = [(attr, value) for attr, value in kwargs.items()]        where_clauses = [f'{attr} = ?' for attr, _ in attr_value_pairs]       where_clause = ' AND '.join(where_clauses)       delete_by_attrs_sql = f'DELETE FROM {object_type_name} WHERE {where_clause};'       values = tuple(value for _, value in attr_value_pairs)        cursor.execute(delete_by_attrs_sql, values)       conn.commit()       conn.close()    def filter(self, **kwargs):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()        object_type_name = self.object_type.__name__        attr_value_pairs = [(attr, value) for attr, value in kwargs.items()]        where_clauses = [f'{attr} = ?' for attr, _ in attr_value_pairs]       where_clause = ' AND '.join(where_clauses)       select_by_attrs_sql = f'SELECT * FROM {object_type_name} WHERE {where_clause};'        values = tuple(value for _, value in attr_value_pairs)        cursor.execute(select_by_attrs_sql, values)       rows = cursor.fetchall()       conn.close()        objects = []       for row in rows:           obj = self.object_type()           for i, value in enumerate(row):               if type(getattr(obj, cursor.description[i][0])) == JsonField:                   setattr(obj, cursor.description[i][0], json.loads(value))               elif type(getattr(obj, cursor.description[i][0])) == ForeignKey:                   value = json.loads(value)                   setattr(obj, cursor.description[i][0],                           EXTERN_TYPES[value['type']].objects.get(**{value['key']: value['value']}))               else:                   setattr(obj, cursor.description[i][0], value)            objects.append(obj)        return objects    def all(self):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()        object_type_name = self.object_type.__name__       select_all_sql = f'SELECT * FROM {object_type_name};'        cursor.execute(select_all_sql)       rows = cursor.fetchall()       conn.close()        objects = []       for row in rows:           obj = self.object_type()           for i, value in enumerate(row):               if type(getattr(obj, cursor.description[i][0])) == JsonField:                   setattr(obj, cursor.description[i][0], json.loads(value))               elif type(getattr(obj, cursor.description[i][0])) == ForeignKey:                   value = json.loads(value)                   setattr(obj, cursor.description[i][0],                           EXTERN_TYPES[value['type']].objects.get(**{value['key']: value['value']}))               else:                   setattr(obj, cursor.description[i][0], value)           objects.append(obj)        return objects    def __createTable__(self):       conn = sqlite3.connect(db_name)       cursor = conn.cursor()       custom_fields = []       for key, value in vars(self.object_type).items():           if not key.startswith(\"__\") and not callable(value):               field_name = key               field_type = value.field_type               is_unique = value.unique               is_null = value.null               default_value = value.default                if value.field_type == 'FOREIGN_KEY':                   field_type = \"TEXT\"               if value.field_type == 'JSON':                   field_type = 'TEXT'                field_declaration = [f'\"{field_name}\" {field_type}']                if is_unique:                   field_declaration.append('UNIQUE')               if not is_null:                   field_declaration.append('NOT NULL')               if default_value is not None:                   field_declaration.append(f'DEFAULT {default_value}')                custom_fields.append(' '.join(field_declaration))        create_table_sql = f'''       CREATE TABLE IF NOT EXISTS {self.object_type.__name__} (           {\", \".join(custom_fields)}       );       '''       cursor.execute(create_table_sql)        conn.commit()       conn.close()<\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0421\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/font><\/h2>\n<p>  \u0421\u0435\u0439\u0447\u0430\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 all() \u0438 filter() \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 list \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u042d\u0442\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e, \u0432\u0435\u0434\u044c \u043d\u0435\u043b\u044c\u0437\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u0418\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u044d\u0442\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043a\u043b\u0430\u0441\u0441 ListOfObjects:<\/p>\n<pre><code class=\"python\">class ListOfObjects:   def __init__(self, objects):       self.objects = objects    def filter(self, **kwargs):       filtered_objects = []       for obj in self.objects:           if all(getattr(obj, attr, None) == value for attr, value in kwargs.items()):               filtered_objects.append(obj)       return ListOfObjects(filtered_objects)    def delete(self):       for obj in self.objects:           obj.delete()    def json(self):       object_dicts = [obj.json() for obj in self.objects]       return object_dicts<\/code><\/pre>\n<p>  <\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u041f\u0440\u0438\u043c\u0435\u0440\u044b<\/font><\/h2>\n<p>  <i>models.py<\/i><\/p>\n<pre><code class=\"python\">import simple_orm.models as models from simple_orm.models import IntegerField, TextField, ForeignKey, JsonField from simple_orm.models import simple_orm from pathlib import Path import sys  PATH = Path(__file__).absolute().parent.parent sys.path.append(str(PATH))   @simple_orm class Box(models.Model): name = TextField() width = IntegerField() height = IntegerField()   @simple_orm class Circle(models.Model): box = ForeignKey(object_class=Box, foreign_field='name') name = TextField() radius = IntegerField() data = JsonField()<\/code><\/pre>\n<p>  <i>main.py<\/i><\/p>\n<pre><code class=\"python\">from models import Box, Circle  box = Box.objects.add(Box('BOX 1', 1, 1)) circle = Circle.objects.add(Circle(box, \"CIRCLE 1\", 5, {'data': 5})) print(circle.json())  print(Box.objects.filter(width=1, height=1).json()) print(Circle.objects.get(name=\"CIRCLE 1\").json()) Box.objects.delete(name=\"BOX 1\") print(Box.objects.all().json())<\/code><\/pre>\n<p>  <\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/font><\/h2>\n<p>  \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e ORM \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u044f\u0437\u044b\u043a\u0430 python, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439 \u043a\u043e\u0434, \u0440\u0435\u0448\u0430\u044e\u0449\u0438\u0439 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<br \/>  <i>\u0414\u0430, \u043e\u043d \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0439, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/i><\/p>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0435\u0441\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/leo-need-more-coffee\/simple-orm\">https:\/\/github.com\/leo-need-more-coffee\/simple-orm<\/a><br \/>  \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f python: <a href=\"https:\/\/pypi.org\/project\/sqlite3-simple-orm\">https:\/\/pypi.org\/project\/sqlite3-simple-orm<\/a><br \/>  \u0421\u043a\u0430\u0447\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pip: pip install sqlite3-simple-orm<\/p>\n<blockquote><p><b><font color=\"#3AC1EF\"><a href=\"https:\/\/t.me\/ruvds_community\">\u0423\u0437\u043d\u0430\u0432\u0430\u0439\u0442\u0435 \u043e \u043d\u043e\u0432\u044b\u0445 \u0430\u043a\u0446\u0438\u044f\u0445 \u0438 \u043f\u0440\u043e\u043c\u043e\u043a\u043e\u0434\u0430\u0445 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e Telegram-\u043a\u0430\u043d\u0430\u043b\u0430 ?<\/a><\/font><\/b><\/p><\/blockquote>\n<p><a href=\"http:\/\/ruvds.com\/ru-rub?utm_source=habr&amp;utm_medium=article&amp;utm_campaign=pzrnqt1vrss&amp;utm_content=%20prostoj_orm_dlya_sqlite3\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/os\/g2\/ms\/osg2msglmcba81cfkh0vl0nlnu4.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/os\/g2\/ms\/osg2msglmcba81cfkh0vl0nlnu4.jpeg\" data-blurred=\"true\"\/><\/a><\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/766552\/\"> https:\/\/habr.com\/ru\/articles\/766552\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><a href=\"https:\/\/habr.com\/ru\/company\/ruvds\/blog\/766552\/\"><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/wn\/sj\/kg\/wnsjkgsyk9aexkxg04zlrar07zo.png\" data-src=\"https:\/\/habrastorage.org\/webt\/wn\/sj\/kg\/wnsjkgsyk9aexkxg04zlrar07zo.png\"\/><\/div>\n<p><\/a><br \/>  ORM, \u0438\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e-\u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u044b. \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u0438\u0441\u0430\u0442\u044c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ORM, \u0447\u0442\u043e\u0431\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435.<\/p>\n<p>  \u041d\u0435 \u0431\u044b\u0432\u0430\u043b\u043e \u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438\u0437\u043d\u0443\u0442\u0440\u0438 \u0442\u0430\u043a\u0430\u044f \u0438\u0434\u0435\u0439\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f? \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0440\u0430\u0431\u043e\u0442\u044b? \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c ORM \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b python \u043d\u0430\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f.<\/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-357120","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357120","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=357120"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357120\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}