<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Denys Rozlomii on Medium]]></title>
        <description><![CDATA[Stories by Denys Rozlomii on Medium]]></description>
        <link>https://medium.com/@denisrozlomiy?source=rss-75daef95a896------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KVbq1ILgNGqw070vuWjvgQ@2x.jpeg</url>
            <title>Stories by Denys Rozlomii on Medium</title>
            <link>https://medium.com/@denisrozlomiy?source=rss-75daef95a896------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 01:12:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@denisrozlomiy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Django Pluggable Architecture — Part 1]]></title>
            <link>https://medium.com/@denisrozlomiy/django-pluggable-architecture-part-1-6fb7d0bb3d78?source=rss-75daef95a896------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fb7d0bb3d78</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[monolithic-architecture]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[architecture]]></category>
            <dc:creator><![CDATA[Denys Rozlomii]]></dc:creator>
            <pubDate>Sun, 10 Nov 2024 16:37:15 GMT</pubDate>
            <atom:updated>2024-11-15T14:27:06.922Z</atom:updated>
            <content:encoded><![CDATA[<h3>Django Pluggable Architecture — Part 1</h3><p>Django is one of the most popular frameworks out there in the Python world, with a whopping 80.7k stars on GitHub as I’m writing this. I’ve worked on tons of Django projects, but I’ve often felt something was missing — a solid, well-organized architecture. Most projects end up either as one big app with folders for models and views or as a few separate apps, following Django’s documentation — but rarely do they go beyond that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6X47JLs9jw0hMqGHTYBcnw.png" /></figure><p>Why is that? It’s a question I’ve pondered a lot. I’ve even had some pretty intense debates with colleagues about the “right” way to structure a Django app. But guess what? These conversations rarely led to a clear answer.</p><p>I also often hear people say, “You can start with Django for your startup, but at some point, you’ll need to switch to microservices.” But… is that really necessary? I’m not so sure. Microservices have their place, sure, but they often add a layer of complexity that isn’t needed for many projects. In fact, a well-thought-out monolith can be just as effective — especially when it’s modular.</p><p>So, today, let’s dive into a modular approach for Django that brings out the best in a monolithic structure while keeping things organized and flexible.</p><h3>Understanding the Problem with Tightly Coupled Modules</h3><p>Most folks know about the MVC (Model-View-Controller) pattern, which Django tweaks a bit into MTV (Model-Template-View). According to this pattern, you’re supposed to put all the business logic in the Model. That setup is fine for basic CRUD apps, but things can get messy fast as your app grows.</p><p>For example, you might start with one model importing another, and soon you’re dealing with tightly coupled modules. Before long, you’ll notice cross-imports popping up all over the place, and suddenly, you’ve got a tangled codebase that’s a nightmare to modify. At this point, someone might suggest splitting everything into microservices. But… is that really the only solution?</p><h3>Default Django Application Structure</h3><p>Before we dive into a better way to structure things, let’s take a quick look at what a typical Django app setup looks like:</p><pre>.<br>├── apps<br>│   ├── __init__.py<br>│   ├── companies/<br>│   ├── jobs/<br>│   └── users/<br>├── job_board<br>│   ├── __init__.py<br>│   ├── asgi.py<br>│   ├── settings.py<br>│   ├── urls.py<br>│   └── wsgi.py<br>└── manage.py</pre><p>And if we look inside each app, it usually follows a basic structure like this:</p><pre>├── jobs<br>│   ├── __init__.py<br>│   ├── admin.py<br>│   ├── apps.py<br>│   ├── migrations/<br>│   │   └── __init__.py<br>│   ├── models.py<br>│   ├── tests.py<br>│   └── views.py</pre><p>It’s simple, and it works… until it doesn’t. As apps grow, they start depending on each other, and you end up with tight coupling — each app relies on the others in ways that make the code tricky to refactor or untangle down the road. This kind of setup is like a web that only gets messier as the project grows.</p><h3>Introducing a Pluggable Approach with Stevedore</h3><p>To clean up our architecture, we can swap out those messy physical dependencies between modules for logical ones. The magic here is a pluggable approach, and for that, I’ll be using the stevedore library from OpenStack.</p><p>So, what does stevedore actually do? In simple terms, it lets you break down your app into separate, pluggable modules that talk to each other through APIs. Think of it as a microservice-style setup, but without the hassle of network requests—just direct function calls. This approach gives you the best of both worlds: the flexibility of microservices with the simplicity of a monolithic app. Pretty cool, right?</p><h3>Step-by-Step Implementation of Stevedore in Django</h3><p>First things first — let’s get stevedore installed:</p><pre>pip install stevedore</pre><p>After that, let’s tweak our application structure a bit to make things pluggable:</p><pre>.<br>├── apps<br>│   ├── __init__.py<br>│   ├── users/<br>│   └── companies/<br>│       ├── __init__.py<br>│       ├── setup.py<br>│       └── src/<br>│           ├── __init__.py<br>│           ├── admin.py<br>│           ├── apps.py<br>│           ├── migrations/<br>│           │   └── __init__.py<br>│           ├── models.py<br>│           ├── pluggable_interface.py<br>│           ├── tests.py<br>│           └── views.py<br>│   ├── jobs/<br>├── job_board/<br>│   ├── __init__.py<br>│   ├── asgi.py<br>│   ├── settings.py<br>│   ├── urls.py<br>│   └── wsgi.py<br>└── manage.py</pre><p>Here, we’ve moved each app’s code into a src/ folder and added setup.py and pluggable_interface.py files. This setup lets us make each module pluggable, helping us keep everything neat and modular.</p><h4>A Closer Look at the Setup Files</h4><p><strong>setup.py</strong></p><p>This file lets us treat each module like a standalone Python package:</p><pre>from setuptools import find_packages, setup<br><br><br>setup(<br>    name=&#39;companies-service&#39;,<br>    version=&#39;1.0&#39;,<br>    description=&#39;All-in-one companies service&#39;,<br>    author=&#39;Denys Rozlomii&#39;,<br>    author_email=&#39;example@example.com&#39;,<br>    classifiers=[<br>        &#39;Development Status :: 3 - Alpha&#39;,<br>        &#39;License :: OSI Approved :: Apache Software License&#39;,<br>        &#39;Programming Language :: Python :: 3.9&#39;,<br>        &#39;Programming Language :: Python :: 3.10&#39;,<br>        &#39;Programming Language :: Python :: 3.11&#39;,<br>        &#39;Programming Language :: Python :: 3.12&#39;,<br>        &#39;Intended Audience :: Developers&#39;,<br>        &#39;Environment :: Console&#39;,<br>    ],<br>    platforms=[&#39;Any&#39;],<br>    provides=[&#39;companies.api&#39;],<br>    packages=find_packages(),<br>    include_package_data=True,<br>    entry_points={<br>        &#39;companies.api&#39;: [<br>            &#39;api = companies.src.pluggable_interface:PluggableAPI&#39;,<br>        ],<br>    },<br>    zip_safe=False,<br>)</pre><p>This setup script configures the module for easy installation. For more on what each part does, check out the stevedore documentation.</p><h4>pluggable_interface.py</h4><p>This file is where we define the API interface that other modules can use to interact with this module. It’s following the pluggable pattern, so we keep all imports self-contained, reducing coupling:</p><pre>from .models import Company<br><br><br>class PluggableAPI:<br>    @staticmethod<br>    def get_recruiters(company_id):<br>        return Company.objects.filter(<br>            id=company_id<br>        ).last().recruiter_set.values_list(&#39;id&#39;, flat=True)<br>    @staticmethod<br>    def get_companies():<br>        return Company.objects.all().values(&#39;id&#39;, &#39;name&#39;)</pre><p>Here, we’re building a clean, pluggable API interface, allowing other modules to access company data without creating direct dependencies. By keeping everything within the module, we maintain loose coupling and a more flexible structure.</p><h4>Updating settings.py</h4><p>To register the pluggable modules, update INSTALLED_APPS in settings.py like this:</p><pre>INSTALLED_APPS = [<br>    ...<br>    &quot;job_board.apps.companies.src.CompaniesConfig&quot;,<br>    &quot;job_board.apps.users.src.UsersConfig&quot;,<br>    &quot;job_board.apps.jobs.src.JobsConfig&quot;,<br>]</pre><h4>Configuring apps.py in Each App</h4><p>Each app’s apps.py file should look something like this:</p><pre>from django.apps import AppConfig<br><br><br>class CompaniesConfig(AppConfig):<br>    default_auto_field = &quot;django.db.models.BigAutoField&quot;<br>    name = &quot;apps.companies.src&quot;<br>    label = &quot;companies&quot;</pre><h4>Calling the API from Other Services</h4><p>Now that we’ve got our modules set up, let’s see how to call the API from another service. Here’s a quick example:</p><p><strong>apps/users/src/views.py</strong></p><pre>from django.shortcuts import render, redirect<br>from chassis.apimanage import build_driver_api<br>from .models import Recruiter<br><br><br>def list_users(request):<br>    users = Recruiter.objects.all()<br>    company_api = build_driver_api(&#39;companies.api&#39;, &#39;api&#39;)<br>    companies_list = company_api.get_companies()<br>    return render(<br>        request,<br>        &#39;users.html&#39;,<br>        context={&#39;users&#39;: users, &#39;companies&#39;: companies_list}<br>    )<br>...<br>def change_company(request, user_id):<br>    recruiter_id = user_id<br>    new_company_id = request.POST.get(&#39;company&#39;)<br>    new_jobs_owner_id = request.POST.get(&#39;new_jobs_owner&#39;)<br>    job_api = build_driver_api(&#39;jobs.api&#39;, &#39;api&#39;)<br>    company_api = build_driver_api(&#39;companies.api&#39;, &#39;api&#39;)<br>    old_jobs_owner = Recruiter.objects.filter(id=recruiter_id).last()<br>    new_jobs_owner = None<br>    if new_jobs_owner_id:<br>        new_jobs_owner = Recruiter.objects.filter(id=new_jobs_owner_id).last()<br>    if not new_jobs_owner:<br>        company_recruiter_ids = company_api.get_recruiters(<br>            old_jobs_owner.company_id<br>        )<br>        if company_recruiter_ids:<br>            new_jobs_owner = Recruiter.objects.filter(<br>                id__in=company_recruiter_ids<br>            ).exclude(id=recruiter_id).last()<br>    job_api.change_jobs_owner(<br>        old_recruiter_id=recruiter_id,<br>        new_recruiter_id=new_jobs_owner.id if new_jobs_owner else None<br>    )<br>    old_jobs_owner.company_id = new_company_id<br>    old_jobs_owner.save()<br>    return redirect(&#39;users&#39;)</pre><p>Notice how we avoid passing Django model instances or QuerySets between modules. Instead, we use Django’s .values() method to get dictionaries, keeping our modules loosely coupled. Using dictionaries also lets us access data in templates with dot notation, which makes handling data in HTML much easier!</p><h3>Conclusion</h3><p>That’s everything I wanted to cover in Part 1. The main idea is to create loosely coupled modules that communicate through APIs, promoting a pluggable architecture. This approach allows each module to operate independently, achieving a balance between microservices and a monolithic structure.</p><p>In doing so, we’ve shifted from <strong>physical</strong> dependencies to <strong>logical</strong> dependencies, creating a more flexible and maintainable architecture.</p><p>For a full working example, you can find the code on my <a href="https://github.com/EightyEighth/django-pluggable-architecture/tree/part1">GitHub</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*290OHjoRKczonfjjDpypEQ.png" /><figcaption>Logical dependencies</figcaption></figure><p>Of course, this approach isn’t a cure-all — it has its limitations. But I really like the idea and wanted to share it with you! In the next parts, we’ll take things further and explore ways to make this architecture even more modular and robust. Till next time!</p><blockquote><strong><em>Note:</em></strong><em> All code provided here is for demonstration purposes only and may not be production-ready.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fb7d0bb3d78" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>