{"id":123,"date":"2018-08-29T12:27:50","date_gmt":"2018-08-29T12:27:50","guid":{"rendered":"https:\/\/159.69.80.24\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/"},"modified":"2025-09-12T15:16:51","modified_gmt":"2025-09-12T15:16:51","slug":"merging-django-orm-with-sqlalchemy-for-easier-data-analysis","status":"publish","type":"post","link":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/","title":{"rendered":"Merging  Django ORM with SQLAlchemy for Easier Data Analysis (Video Inside)"},"content":{"rendered":"<p>Django development of products is usually easy and straightforward: great documentation, many tools out of the box, plenty of open source libraries and big community. Django ORM takes full control about SQL layer protecting you from mistakes, and underlying details of queries so you can spend more time on designing and building your application structure in Python code.<\/p>\n<p>However, sometimes such behavior may hurt &#8211; for example, when Django developers are building a project related to data analysis. Building advanced queries with Django is not very easy; it\u2019s hard to read (in Python) and hard to understand what\u2019s going on in SQL-level without logging or printing generated SQL queries somewhere. Moreover, such queries could not be efficient enough, so this will hit you back when you load more data into DB to play with.<\/p>\n<p>In one moment, you can find yourself doing too much raw SQL through Django cursor, and this is the moment when you should do a break and take a look on another interesting tool, which is placed right between ORM layer and the layer of raw SQL queries.<\/p>\n<p>As you can see from the title of the article, we successfully mixed Django ORM and SQLAlchemy Core together, and we\u2019re very satisfied with results. We built an application which helps to analyze data produced by EMR systems by aggregating data into charts and tables, scoring by throughput\/efficiency\/staff cost, and highlighting outliers which allows to optimize business processes for clinics and save money. Well, let&#8217;s learn more about the integration of Django with SQLAlchemy.<\/p>\n<p>By the way, our Django specialists have extensive experience in using Django and SQLAlchemy. Therefore, if you need additional advice or assistance in developing a project, please contact us or see our <a href=\"https:\/\/djangostars.com\/case-studies\/\">portfolio<\/a>.<\/p>\n<h2>What is the point of mixing Django ORM with SQLAlchemy?<\/h2>\n<p>There are a few reasons why we stepped out from Django ORM for this task:<\/p>\n<ul>\n<li>For ORM world, one object is one record in the database, but here we deal only with aggregated data.<\/li>\n<li>Some aggregations are very tricky, and Django ORM functionality is not enough to fulfill the needs. To be honest, sometimes in some simple cases it\u2019s hard (or even impossible) to make ORM produce the SQL query exactly the way you want, and when you\u2019re dealing with a <strong>big data<\/strong>, it will affect Django performance a lot.<\/li>\n<li>If you\u2019re building advanced queries via Django ORM, it\u2019s hard to read and understand such queries in Python, and hard to predict which SQL query will be generated and treated to the database.<\/li>\n<\/ul>\n<p>It\u2019s worth saying that we also set up a second database, which is handled by Django ORM to cover other web application related tasks and business-logic needs, which it perfectly does. Django ORM is evolving from version to version, giving more and more features. For a <a href=\"https:\/\/djangostars.com\/blog\/how-to-update-the-django-version-a-step-by-step-guide-to-hassle-free-upgrades\/\">Django upgrade<\/a>, keep analytics in a decoupled SQLAlchemy Core layer\u2014often on a separate database\u2014so you can adopt new Django releases without rewriting domain models.<\/p>\n<p>For example, in recent releases, a bunch of neat features were added like support of <a href=\"https:\/\/docs.djangoproject.com\/en\/1.11\/ref\/models\/expressions\/#subquery-expressions\" rel=\"nofollow\">Subquery expressions<\/a> or <a href=\"https:\/\/docs.djangoproject.com\/en\/2.1\/ref\/models\/database-functions\/#window-functions\" rel=\"nofollow\">Window functions<\/a> and many others, which you should definitely try before doing raw SQL or looking at the tools like SQLAlchemy if your problem is more complex than fixing a few queries.<\/p>\n<p>So that\u2019s why we decided to take a look at SQLAlchemy. It consists of two parts &#8211; ORM and Core. SQLAlchemy ORM is similar to Django ORM, but at the same time, they differ. SQLAlchemy ORM uses a different concept, Data Mapper, compared to Django&#8217;s Active Record approach. As far as you\u2019re building projects on Django, you definitely should not switch ORM (if you don\u2019t have very special reasons to do so), as you want to use Django REST framework, Django-admin, and other neat stuff which is tied to Django models. In <a href=\"https:\/\/djangostars.com\/blog\/flask-vs-django\/\">Django vs Flask<\/a> evaluations, use Django\u2019s ORM for app workflows and layer in SQLAlchemy Core for complex, aggregation-heavy analytics\u2014gaining both productivity and explicit, readable SQL where it counts.<br \/>\nThe second part of SQLAlchemy is called Core. It\u2019s placed right between high-level ORM and low-level SQL. The Core is very powerful and flexible; it gives you the ability to build any SQL-queries you wish, and when you see such queries in Python, it\u2019s easy to understand what\u2019s going on. For example, take a look into a sample query from the documentation:<\/p>\n<pre><code class=\"language- python\">q = session.query(User).filter(User.name.like('e%')).\r\n    limit(5).from_self().\r\n    join(User.addresses).filter(Address.email.like('q%')).\r\n    order_by(User.name)\r\n<\/code><\/pre>\n<p>Which will result into<\/p>\n<pre><code class=\"language-python\">SELECT anon_1.user_id AS anon_1_user_id,\r\n       anon_1.user_name AS anon_1_user_name\r\nFROM (SELECT \"user\".id AS user_id, \"user\".name AS user_name\r\nFROM \"user\"\r\nWHERE \"user\".name LIKE :name_1\r\n LIMIT :param_1) AS anon_1\r\nJOIN address ON anon_1.user_id = address.user_id\r\nWHERE address.email LIKE :email_1 ORDER BY anon_1.user_name\r\n<\/code><\/pre>\n<p>Note: with such tricks, we don\u2019t fall into <code>N+1 problem<\/code>: <code>from_select<\/code> makes an additional <code>SELECT<\/code> wrapper around the query, so we reduce the amount of rows at first (via <code>LIKE<\/code> and <code>LIMIT<\/code>) and only then we join the address information.<\/p>\n<h2>How to mix Django and SQLALchemy<\/h2>\n<p>So if you\u2019re interested and want to try to mix SQLAlchemy with Django application, here are some hints which could help you. You can use this instruction as a tutorial for using SQLALchemy with Django.<br \/>\nFirst of all, you need to <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/engines.html#engine-configuration\" rel=\"nofollow\">create<\/a> a global variable with Engine, but the actual connection with DB will be established on first <code>connect<\/code> or <code>execute<\/code> call.<\/p>\n<pre><code class=\"language-python\">sa_engine = create_engine(settings.DB_CONNECTION_URL, pool_recycle=settings.POOL_RECYCLE)\r\n<\/code><\/pre>\n<p><code>Create_engine<\/code> accepts additional configuration for connection. MySQL\/MariaDB\/AWS Aurora (MySQL compatible) have an interactive_timeout setting which is 8h by default, so without <code>pool_recycle<\/code> extra parameter you will get annoying <code>SQLError: (OperationalError) (2006, \u2018MySQL server has gone away\u2019)<\/code>. So the <code>POOL_RECYCLE<\/code> should be smaller than <code>interactive_timeout<\/code>. For example a half of it: <code>POOL_RECYCLE = 4 * 60 * 60<\/code><br \/>\nNext step is to build your queries. Depending on your application architecture, you can declare tables and fields <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/metadata.html\" rel=\"nofollow\">using<\/a> <code>Table<\/code> and <code>Column<\/code> classes (which also can be used with ORM), or if your application already stores tables and columns names in another way, you can do it in place, via <code>Table<\/code>\u00a0and <code>Column<\/code> functions (as shown <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/selectable.html#sqlalchemy.sql.expression.table\" rel=\"nofollow\">here<\/a>).<br \/>\nIn our app, we picked the second option, as we stored information about aggregations, formulas, and formatting in our own declarative syntax. Then we built a layer which read such structures and executed queries based on the provided instructions.<br \/>\nWhen your query is ready, simply call <code>sa_engine.execute(query)<\/code>. The cursor would be opened until you read all the data or if you close it explicitly.<br \/>\nThere is one very annoying thing worth mentioning. As a <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/faq\/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined\" rel=\"nofollow\">documentation says<\/a>, SQLAlchemy in Django has limited ability to do query stringification, so it\u2019s not so easy to get a final query which will be executed. You can print query by itself:<\/p>\n<pre><code class=\"language-python\">print(query)\r\nSELECT\r\nrole_group_id, role_group_name, nr_patients\r\nFROM \"StaffSummary\"\r\nWHERE day &gt;= :day_1 AND day &lt;= :day_2 AND location_id = :location_id_1 AND service_id = :service_id_1\r\n<\/code><\/pre>\n<p>(This one looks not so scary, but with more complex queries it could be about 20+ placeholders, which are very annoying and time-expensive to fill manually to play later in SQL console.)<br \/>\nIf you have only strings and numbers to be inserted into query, this will work for you<\/p>\n<pre><code class=\"language-python\">print(s.compile(compile_kwargs={\"literal_binds\": True}))\r\n<\/code><\/pre>\n<p>For dates, such a trick will not work. There is <a href=\"https:\/\/stackoverflow.com\/questions\/5631078\/sqlalchemy-print-the-actual-query\" rel=\"nofollow\">a discussion<\/a> on StackOverflow on how to achieve the desired results, but solutions look unattractive.<br \/>\nAnother option is to enable queries logging into the file via database configuration, but in this case, you could face another issue; it becomes hard to find a query you want to debug if Django ORM connected to this database too.<\/p>\n<h2>Testing<\/h2>\n<p>Note: Pytest <a href=\"https:\/\/pytest-django.readthedocs.io\/en\/latest\/database.html#tests-requiring-multiple-databases\" rel=\"nofollow\">multidb note<\/a> says \u201cCurrently pytest-django does not specifically support Django\u2019s multi-database support. You can however use normal Django TestCase instances to use it\u2019s multi_db support.\u201d<br \/>\nSo what does it mean &#8211; not support? By default, Django will create and remove (at the end of all tests) a test-database for each db listed in <code>DATABASES<\/code> definition. This feature works perfectly with <code>pytests<\/code> also.<br \/>\nDjango <code>TestCase<\/code> and <code>TransactionTestCase<\/code> with <code>multi_db=True<\/code> enables erasing of data in multiple databases between tests. Also it enables data loading into second database via <code>django-fixtures<\/code>, but it\u2019s much better to use <a href=\"https:\/\/model-mommy.readthedocs.io\/en\/latest\/basic_usage.html\" rel=\"nofollow\">model<em>mommy<\/em><\/a> or <a href=\"https:\/\/model-mommy.readthedocs.io\/en\/latest\/basic_usage.html\" rel=\"nofollow\">factoryboy<\/a> instead, which are not affected by this attribute.<br \/>\nThere are a few hacks suggested in <a href=\"https:\/\/github.com\/pytest-dev\/pytest-django\/issues\/76\" rel=\"nofollow\">pytest-django discussion<\/a> how to work around the issue and enable <code>multi_db<\/code> to continue pytesting.<br \/>\nThere is one important advice &#8211; for tables that have Django-models, you should save data to DB via Django-ORM. Otherwise, you will face issues during writing tests. <code>TestCase<\/code> will not be able to rollback other transactions which happened outside from Django DB connection. If you have such a situation, you may use <code>TransactionalTestCase<\/code> with <code>multi_db=True<\/code> for tests which trigger functionality, which produces DB writes through SQLAlchemy connection, but remember that such tests are slower than regular <code>TestCase<\/code>.<br \/>\nAlso, another scenario is possible &#8211; you have Django-models only in one database and you\u2019re working with the second database via SQLAlchemy. In this case, <code>multi_db<\/code> doesn\u2019t affect you at all. In such cases, you need to write a pytest-fixture (or do it as a mixin and trigger logic in <code>setUp<\/code> if you\u2019re using unittests) which will create DB structure from SQL file. Such a file should contain <code>DROP TABLE IF EXISTS<\/code> statements before <code>CREATE TABLE<\/code>. This fixture should be applied to each test case which manipulates with this database. Other fixture could load data into created tables.<br \/>\nNote: Such tests will be slower as tables will be recreated for each test. Ideally, tables should be created once (declared as <code>@pytest.fixture(scope='session', autouse=True)<\/code>), and each transaction should rollback data for each test. It\u2019s not easy to achieve because of different connections: Django &amp; SQLAlchemy or different connections of SQLAlchemy connection-pool, e.g in your tests you start the transaction, fill DB with test data, then run test and rollback transaction (it wasn\u2019t committed). But during the test, your application code may do queries to DB like connection.execute(query) which performed outside of transaction which created test data.<\/p>\n<p>So with default transaction isolation level, the application will not see any data, only empty tables. It\u2019s possible to change transaction isolation level to <code>READ UNCOMMITTED<\/code> for SQLAlchemy connection, and everything will work as expected, but it\u2019s definitely not a solution at all.<\/p>\n<h2>Conclusion<\/h2>\n<p>To sum up everything above, SQLAlchemy Core is a great tool which brings you closer to SQL and gives you understanding and full control over the queries. If you\u2019re building the application (or a part of it) which requires advanced aggregations, it is worth it to check out SQLAlchemy Core capabilities as an alternative to Django ORM tools.<\/p>\n<p>By the way, if you want to hire an SQL Alchemy ORM developer, you can <a href=\"https:\/\/djangostars.com\/get-in-touch\/\">contact<\/a> our <a href=\"https:\/\/djangostars.com\/services\/python-django-development\/\">Django agency<\/a> for advice.<\/p>\n<h3>Edit:<\/h3>\n<p>For a more in-depth dive into the topic, I would recommend to check out my talk on Djangocon Europe 2019.<\/p>\n<p style=\"padding-left: 0px;\"><iframe src=\"https:\/\/www.youtube.com\/embed\/fpc6lkMdKzY\" width=\"640\" height=\"360\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><br \/>\n<!--more--><!--more--><\/p>\n<p>I will be publishing a new post on the topic shortly, so feel free to sign up for the newsletter below to see it as soon as it&#8217;s ready!<br \/>\n<!-- Begin DS MailChimp Signup Form -->\n<div class=\"l-card-subscribe l-card-subscribe--dark ds-form\">\n\t<div class=\"l-card-subscribe__content\">\n        \n        \n        <h3 class=\"l-card-subscribe__title\">Sign up for our newsletter<\/h3>\n\t\t<form class=\"l-card-subscribe__form\" action=\"\" data-id=\"mailchimp\" id=\"\" data-analytics=\"mailchimp-form\">\n\t\t\t<div class=\"l-card-subscribe__form-content\">\n                <input class=\"c-input l-card-subscribe__input ds-form__email\" name=\"email_address\" type=\"text\" placeholder=\"Your email address\" \/>\n\t\t\t\t<button class=\"btn btn-primary ds-form__submit\">subscribe<\/button>\n            <\/div>\n\t\t\t<div class=\"l-card-subscribe__form-content\">\n\t\t\t\t<h3 class=\"l-card-subscribe__succesed-text\"><\/h3>\n\t\t\t\t&nbsp;\n\n\t\t\t<\/div>\n\t\t<\/form><\/div>\n<\/div>\n<!-- end DS MailChimp Signup Form -->\n<\/p>\n<div class=\"article-linking \">\n<div class=\"article-linking-background\" style=\"background-image: url('https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2019\/05\/2_1_bg.png');\"><\/div>\n<div class=\"article-linking-content\">\n<h3 class=\"article-linking-caption\"><u>Python &amp; Django<\/u> development<\/h3>\n<p><!-- it was h2 and got in Table of Content --><\/p>\n<p class=\"article-linking-caption-small\">Your chance to enter the market faster<\/p>\n<div class=\"article-link-wrapper\"><a href=\"https:\/\/djangostars.com\/services\/python-django-development\/\" target=\"_blank\" rel=\"nofollow noopener\">Learn more &gt;<\/a><\/div>\n<\/div>\n<\/div>\n<div class=\"dj-main-article-faq\" style=\"padding-top: 0px;\">\n\t\t<div class=\"dj-main-article-faq-title\">\n\t\tFrequently Asked Questions\n\t\t<\/div>\n\t\t<div class=\"dj-main-article-faq-items\">\n\t\t\t<div class=\"dj-main-article-faq-accordeon accordeon\"><dl>\n\t\t\t\t<dt>What is the difference between SQLAlchemy vs. Django ORM? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>Although SQLAlchemy ORM is similar to Django ORM, it uses a different concept, Data Mapper, compared to Django's Active Record approach. And while Django ORM interacts with raw SQL via a non-public API, SQLAlchemy Core is placed between SQLAlchemy ORM layer and the layer of raw SQL queries.<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>Does Django use SQLAlchemy as ORM? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>SQLAlchemy is a library that facilitates the communication between Python programs and databases using ORM (Object-Relational Mapping) technology. Thus, it's an alternative to Django ORM that allows interaction with databases using high-level Python methods instead of SQL queries.<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>Is SQLAlchemy better than Django ORM? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>Building advanced queries with Django ORM is not so easy. It\u2019s hard to read them in Python and to understand what\u2019s going on at the SQL level, especially dealing with big databases. SQLAlchemy consists of two parts \u2013 ORM and Core. SQLAlchemy Core, placed between high-level ORM and low-level SQL, is powerful and flexibl\u0435. It helps to write more precise SQL queries that are easier to understand when reading Python code.<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>Should I use SQLAlchemy core or ORM? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>In most cases, the capabilities of Django ORM are sufficient. SQLAlchemy Core can be more convenient when: your app mostly works with aggregations, you have a lot of data, you need precise and performant queries, you're transforming complex queries from SQL to Python, you're building advanced queries dynamically, or the database is not natively supported by Django (e.g., SQL Azure, Sybase, Firebird).<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>How to hire an SQL Alchemy ORM developer? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>SQL Alchemy is a Python library that allows building queries to databases without SQL (thanks to ORM technology). Therefore, working with it requires strong knowledge of Python. Our developers have extensive experience with Python and Django - Django Stars' portfolio includes over 120 projects. We will be glad if you <a href=\"https:\/\/djangostars.com\/get-in-touch\/\">contact us<\/a> to discuss your project.<\/dd>\n\t\t\t<\/dl><\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django development of products is usually easy and straightforward: great documentation, many tools out of the box, plenty of open source libraries and big community. Django ORM takes full control about SQL layer protecting you from mistakes, and underlying details of queries so you can spend more time on designing and building your application structure [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":3541,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[44],"tags":[30],"class_list":["post-123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-django","tag-backend"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Software Development Blog &amp; IT Tech Insights | Django Stars<\/title>\n<meta name=\"description\" content=\"Learn how to simplify building advanced queries for complex data analysis. Discover how we integrated Django ORM and SQLAlchemy Core and the results it delivered.\" \/>\n<link rel=\"canonical\" href=\"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/123\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merging Django ORM with SQLAlchemy (Video Inside) | Django Stars\" \/>\n<meta property=\"og:description\" content=\"Learn how to simplify building advanced queries for complex data analysis. Discover how we integrated Django ORM and SQLAlchemy Core and the results it delivered.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/\" \/>\n<meta property=\"og:site_name\" content=\"Software Development Blog &amp; IT Tech Insights | Django Stars\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/djangostars\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/gleb.pushkov\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-29T12:27:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-12T15:16:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Gleb Pushkov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@djangostars\" \/>\n<meta name=\"twitter:site\" content=\"@djangostars\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gleb Pushkov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/\"},\"author\":{\"name\":\"Gleb Pushkov\",\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f7cde5adf51d6959ce4fd91a63a8e1c0\"},\"headline\":\"Merging Django ORM with SQLAlchemy for Easier Data Analysis (Video Inside)\",\"datePublished\":\"2018-08-29T12:27:50+00:00\",\"dateModified\":\"2025-09-12T15:16:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/\"},\"wordCount\":1756,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg\",\"keywords\":[\"Backend\"],\"articleSection\":[\"Python &amp; Django\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/\",\"url\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/\",\"name\":\"Merging Django ORM with SQLAlchemy (Video Inside) | Django Stars\",\"isPartOf\":{\"@id\":\"https:\/\/djangostars.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg\",\"datePublished\":\"2018-08-29T12:27:50+00:00\",\"dateModified\":\"2025-09-12T15:16:51+00:00\",\"author\":{\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f7cde5adf51d6959ce4fd91a63a8e1c0\"},\"description\":\"Learn how to simplify building advanced queries for complex data analysis. Discover how we integrated Django ORM and SQLAlchemy Core and the results it delivered.\",\"breadcrumb\":{\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage\",\"url\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg\",\"contentUrl\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg\",\"width\":1440,\"height\":620,\"caption\":\"Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/djangostars.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Merging Django ORM with SQLAlchemy for Easier Data Analysis (Video Inside)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/djangostars.com\/blog\/#website\",\"url\":\"https:\/\/djangostars.com\/blog\/\",\"name\":\"Software Development Blog &amp; IT Tech Insights | Django Stars\",\"description\":\"Welcome behind the scenes of software product development. We share our best practices, tech solutions, management tips, and every useful insight we\u2018ve got while working on our projects.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/djangostars.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f7cde5adf51d6959ce4fd91a63a8e1c0\",\"name\":\"Gleb Pushkov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7a2860daefba91fbf5b559556d8cbca79ba7de4c285487deb7a733e1a9666e15?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7a2860daefba91fbf5b559556d8cbca79ba7de4c285487deb7a733e1a9666e15?s=96&d=mm&r=g\",\"caption\":\"Gleb Pushkov\"},\"sameAs\":[\"https:\/\/www.facebook.com\/gleb.pushkov\",\"https:\/\/www.linkedin.com\/in\/gleb-pushkov-1238b94b\/\"],\"url\":\"https:\/\/djangostars.com\/blog\/author\/gleb-pushkov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Software Development Blog &amp; IT Tech Insights | Django Stars","description":"Learn how to simplify building advanced queries for complex data analysis. Discover how we integrated Django ORM and SQLAlchemy Core and the results it delivered.","canonical":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/123","og_locale":"en_US","og_type":"article","og_title":"Merging Django ORM with SQLAlchemy (Video Inside) | Django Stars","og_description":"Learn how to simplify building advanced queries for complex data analysis. Discover how we integrated Django ORM and SQLAlchemy Core and the results it delivered.","og_url":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/","og_site_name":"Software Development Blog &amp; IT Tech Insights | Django Stars","article_publisher":"https:\/\/www.facebook.com\/djangostars\/","article_author":"https:\/\/www.facebook.com\/gleb.pushkov","article_published_time":"2018-08-29T12:27:50+00:00","article_modified_time":"2025-09-12T15:16:51+00:00","og_image":[{"width":1440,"height":620,"url":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg","type":"image\/jpeg"}],"author":"Gleb Pushkov","twitter_card":"summary_large_image","twitter_creator":"@djangostars","twitter_site":"@djangostars","twitter_misc":{"Written by":"Gleb Pushkov","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#article","isPartOf":{"@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/"},"author":{"name":"Gleb Pushkov","@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f7cde5adf51d6959ce4fd91a63a8e1c0"},"headline":"Merging Django ORM with SQLAlchemy for Easier Data Analysis (Video Inside)","datePublished":"2018-08-29T12:27:50+00:00","dateModified":"2025-09-12T15:16:51+00:00","mainEntityOfPage":{"@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/"},"wordCount":1756,"commentCount":0,"image":{"@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage"},"thumbnailUrl":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg","keywords":["Backend"],"articleSection":["Python &amp; Django"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/","url":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/","name":"Merging Django ORM with SQLAlchemy (Video Inside) | Django Stars","isPartOf":{"@id":"https:\/\/djangostars.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage"},"image":{"@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage"},"thumbnailUrl":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg","datePublished":"2018-08-29T12:27:50+00:00","dateModified":"2025-09-12T15:16:51+00:00","author":{"@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f7cde5adf51d6959ce4fd91a63a8e1c0"},"description":"Learn how to simplify building advanced queries for complex data analysis. Discover how we integrated Django ORM and SQLAlchemy Core and the results it delivered.","breadcrumb":{"@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#primaryimage","url":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg","contentUrl":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside.jpg","width":1440,"height":620,"caption":"Merging-Django-ORM-with-SQLAlchemy-for-Easier-Data-Analysis-Video-Inside"},{"@type":"BreadcrumbList","@id":"https:\/\/djangostars.com\/blog\/merging-django-orm-with-sqlalchemy-for-easier-data-analysis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/djangostars.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Merging Django ORM with SQLAlchemy for Easier Data Analysis (Video Inside)"}]},{"@type":"WebSite","@id":"https:\/\/djangostars.com\/blog\/#website","url":"https:\/\/djangostars.com\/blog\/","name":"Software Development Blog &amp; IT Tech Insights | Django Stars","description":"Welcome behind the scenes of software product development. We share our best practices, tech solutions, management tips, and every useful insight we\u2018ve got while working on our projects.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/djangostars.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f7cde5adf51d6959ce4fd91a63a8e1c0","name":"Gleb Pushkov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7a2860daefba91fbf5b559556d8cbca79ba7de4c285487deb7a733e1a9666e15?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7a2860daefba91fbf5b559556d8cbca79ba7de4c285487deb7a733e1a9666e15?s=96&d=mm&r=g","caption":"Gleb Pushkov"},"sameAs":["https:\/\/www.facebook.com\/gleb.pushkov","https:\/\/www.linkedin.com\/in\/gleb-pushkov-1238b94b\/"],"url":"https:\/\/djangostars.com\/blog\/author\/gleb-pushkov\/"}]}},"_links":{"self":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":20,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":9694,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/123\/revisions\/9694"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/media\/3541"}],"wp:attachment":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}