{"id":21801,"date":"2024-03-15T22:36:58","date_gmt":"2024-03-15T22:36:58","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/"},"modified":"2024-03-21T21:21:13","modified_gmt":"2024-03-21T21:21:13","slug":"what-is-the-usage-of-the-orm-framework-in-python","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/","title":{"rendered":"What is the usage of the ORM framework in Python?"},"content":{"rendered":"<p>In Python, the ORM (Object Relational Mapping) framework is used to map data from a database to Python objects, allowing programmers to manipulate the database in an object-oriented manner without having to directly write SQL statements.<\/p>\n<p>Common Python ORM frameworks include SQLAlchemy, Django ORM, and Peewee.<\/p>\n<p>The general steps of using an ORM framework are as follows:<\/p>\n<ol>\n<li>Defining model classes: Mapping tables in the database as a class using Python classes, where the class attributes correspond to the table fields, and the class methods correspond to table operations (insert, update, delete, retrieve).<\/li>\n<li>Establishing a database connection: Utilize the API provided by the ORM framework to establish a connection with the database.<\/li>\n<li>Create table structure: Use the methods provided by the ORM framework to generate the corresponding table structure based on the model class.<\/li>\n<li>CRUD operations: performing database operations such as adding, deleting, modifying, and querying by calling methods of the model class. For example, creating an object and saving it to the database, updating object properties and saving them, and querying the database based on certain conditions.<\/li>\n<li>Close the database connection: Disconnect from the database when the program ends.<\/li>\n<\/ol>\n<p>Here is an example using SQLAlchemy as an ORM framework:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">from<\/span> sqlalchemy <span class=\"hljs-keyword\">import<\/span> create_engine, Column, Integer, String\r\n<span class=\"hljs-keyword\">from<\/span> sqlalchemy.orm <span class=\"hljs-keyword\">import<\/span> sessionmaker\r\n<span class=\"hljs-keyword\">from<\/span> sqlalchemy.ext.declarative <span class=\"hljs-keyword\">import<\/span> declarative_base\r\n\r\n<span class=\"hljs-comment\"># \u521b\u5efa\u5f15\u64ce<\/span>\r\nengine = create_engine(<span class=\"hljs-string\">'\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32'<\/span>)\r\n\r\n<span class=\"hljs-comment\"># \u521b\u5efa\u4f1a\u8bdd<\/span>\r\nSession = sessionmaker(bind=engine)\r\nsession = Session()\r\n\r\n<span class=\"hljs-comment\"># \u521b\u5efa\u57fa\u7c7b<\/span>\r\nBase = declarative_base()\r\n\r\n<span class=\"hljs-comment\"># \u5b9a\u4e49\u6a21\u578b\u7c7b<\/span>\r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">User<\/span>(<span class=\"hljs-title class_ inherited__\">Base<\/span>):\r\n    __tablename__ = <span class=\"hljs-string\">'users'<\/span>\r\n    <span class=\"hljs-built_in\">id<\/span> = Column(Integer, primary_key=<span class=\"hljs-literal\">True<\/span>)\r\n    name = Column(String)\r\n    age = Column(Integer)\r\n\r\n<span class=\"hljs-comment\"># \u521b\u5efa\u8868\u7ed3\u6784<\/span>\r\nBase.metadata.create_all(engine)\r\n\r\n<span class=\"hljs-comment\"># \u589e\u5220\u6539\u67e5\u64cd\u4f5c<\/span>\r\nuser = User(name=<span class=\"hljs-string\">'Alice'<\/span>, age=<span class=\"hljs-number\">20<\/span>)\r\nsession.add(user)\r\nsession.commit()\r\n\r\nuser.age = <span class=\"hljs-number\">22<\/span>\r\nsession.commit()\r\n\r\nusers = session.query(User).<span class=\"hljs-built_in\">filter<\/span>(User.age &gt; <span class=\"hljs-number\">18<\/span>).<span class=\"hljs-built_in\">all<\/span>()\r\n<span class=\"hljs-keyword\">for<\/span> user <span class=\"hljs-keyword\">in<\/span> users:\r\n    <span class=\"hljs-built_in\">print<\/span>(user.name, user.age)\r\n\r\n<span class=\"hljs-comment\"># \u5173\u95ed\u4f1a\u8bdd<\/span>\r\nsession.close()\r\n<\/code><\/pre>\n<p>In the above example, the database engine and session are first created, a model class User is defined, then table structure is created based on the model class, followed by CRUD operations. Finally, the session is closed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the ORM (Object Relational Mapping) framework is used to map data from a database to Python objects, allowing programmers to manipulate the database in an object-oriented manner without having to directly write SQL statements. Common Python ORM frameworks include SQLAlchemy, Django ORM, and Peewee. The general steps of using an ORM framework are [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-21801","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is the usage of the ORM framework in Python? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is the usage of the ORM framework in Python?\" \/>\n<meta property=\"og:description\" content=\"In Python, the ORM (Object Relational Mapping) framework is used to map data from a database to Python objects, allowing programmers to manipulate the database in an object-oriented manner without having to directly write SQL statements. Common Python ORM frameworks include SQLAlchemy, Django ORM, and Peewee. The general steps of using an ORM framework are [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T22:36:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T21:21:13+00:00\" \/>\n<meta name=\"author\" content=\"Sophia Anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sophia Anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\"},\"author\":{\"name\":\"Sophia Anderson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30\"},\"headline\":\"What is the usage of the ORM framework in Python?\",\"datePublished\":\"2024-03-15T22:36:58+00:00\",\"dateModified\":\"2024-03-21T21:21:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\"},\"wordCount\":246,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\",\"name\":\"What is the usage of the ORM framework in Python? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T22:36:58+00:00\",\"dateModified\":\"2024-03-21T21:21:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is the usage of the ORM framework in Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30\",\"name\":\"Sophia Anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g\",\"caption\":\"Sophia Anderson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/sophiaanderson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is the usage of the ORM framework in Python? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/","og_locale":"en_US","og_type":"article","og_title":"What is the usage of the ORM framework in Python?","og_description":"In Python, the ORM (Object Relational Mapping) framework is used to map data from a database to Python objects, allowing programmers to manipulate the database in an object-oriented manner without having to directly write SQL statements. Common Python ORM frameworks include SQLAlchemy, Django ORM, and Peewee. The general steps of using an ORM framework are [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T22:36:58+00:00","article_modified_time":"2024-03-21T21:21:13+00:00","author":"Sophia Anderson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Sophia Anderson","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/"},"author":{"name":"Sophia Anderson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30"},"headline":"What is the usage of the ORM framework in Python?","datePublished":"2024-03-15T22:36:58+00:00","dateModified":"2024-03-21T21:21:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/"},"wordCount":246,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/","url":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/","name":"What is the usage of the ORM framework in Python? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T22:36:58+00:00","dateModified":"2024-03-21T21:21:13+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-orm-framework-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is the usage of the ORM framework in Python?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30","name":"Sophia Anderson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g","caption":"Sophia Anderson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/sophiaanderson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/21801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=21801"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/21801\/revisions"}],"predecessor-version":[{"id":55684,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/21801\/revisions\/55684"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=21801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=21801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=21801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}