# Django Design Pattern for Large complex web app

**URL:** <https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680>\
**Category:** Getting Started\
**Created:** [April 1, 2024, 8:58am UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680 "2024-04-01T08:58:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinothrajs](https://avatars.discourse-cdn.com/v4/letter/v/65b543/32.png) [@vinothrajs](https://forum.djangoproject.com/u/vinothrajs)\
**Post date:** [April 1, 2024, 8:58am UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/1 "2024-04-01T08:58:06Z")

</div>

I’m new to Django and have read numerous articles about constructing complex web-based applications that incorporate intricate business logic. We anticipate that the application, which we’re rewriting from scratch, will be substantial, involving processes that could take upwards of 300 pages with complex business logic

1. What design pattern should be adopted in Django for effective architecture? For instance:

a. Should each model have a corresponding service layer (service.py) to facilitate the creation of reusable functions that can be utilized across different screens, as suggested on [ilovedjango.com](https://ilovedjango.com/django/orm/django-custom-managers/)?

b. Alternatively, is it advisable to adhere to the concept of managers in Django?

c. Between the service.py and managers.py patterns, which is considered more reliable and maintainable?

d. Additionally, is it necessary to implement Dependency Injection, and if so, does this introduce performance issues? I came across a discussion on this topic at [snyk.io](https://snyk.io/blog/dependency-injection-python/) related to the repository design pattern.

which can we maintainable, testable & better performance

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 1, 2024, 1:11pm UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/2 "2024-04-01T13:11:23Z")

</div>

`<opinion>`  
There aren’t any “universally accepted” patterns for large Django projects. For every individual proposing a “service layer”, you can find an equal number identifying them as unnecessary.

I’m in that latter camp.

Our flagship application consists of 9 apps with more than 120,000 lines of production code, using more than 125 models, a complete custom row-level security model, and a significant number of dynamically generated forms and views.

We have found _no_ need to go beyond or outside Django’s standard project structure. We never even considered the implementation of a “service layer”, because we have never seen the need to extend Django in that direction.

> [@vinothrajs](#):
>
> c. Between the service.py and managers.py patterns, which is considered more reliable and maintainable?

The quality of the code being written on a per-function basis is a _far_ greater factor in reliability and maintainability than any higher-level architectural pattern. Create your coding standards and enforce them rigidly.

> [@vinothrajs](#):
>
> d. Additionally, is it necessary to implement Dependency Injection,

I have never encountered a situation in Python where it is of value. (And yes, I have more than 5 years experience working in a Java / JavaEE / Spring, so I am familiar with the needs for it in that environment. However, the value for it comes from having to work around the limitations of Java, not because of any intrinsic value to the architecture itself.)

> [@vinothrajs](#):
>
> which can we maintainable, testable & better performance

Again, the “tactical” decisions you make regarding specific function implementations are more important here than the strategic architectural decisions. Being truly **proficient** with Python gives you your best opportunities for success.  
`<opinion>`

---

<div class="post-metadata">

**Author:** ![vinothrajs](https://avatars.discourse-cdn.com/v4/letter/v/65b543/32.png) [@vinothrajs](https://forum.djangoproject.com/u/vinothrajs)\
**Post date:** [April 2, 2024, 2:52am UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/3 "2024-04-02T02:52:31Z")

</div>

ok thanks for the detailed explanation, so I can work on managing of required to extend the model I do have a couple of more questions.

1. Do we need to have one model per app or we can have one app with all models created or grouped into logical relationships with different folders

2. Am planning to use htmx for ajax posts in all forms to make feel async and avoid page refresh, is it ok or i need to write an ajax request manually for each, what would be the best approach?

3. Can we use Raw sql only for large and complex and for normal select and update e, delete use form, will that be a problem in the future while scaling, using 2 ways of managing db access?

4. also i tried Ajax form post, now how do we attach multiple models in a single form in Django and show in UI and do post and validation using ajax, the form needs to be single but the fields inside that form will come in various form

5. I tried Abstractbaseuser but i would need the auth and authorize mechanism to handle dynamically from the table base don each URL so that i can handle that in middleware, rather than going with the decorator on each function in view, is it fine can we write entire auth and authorize without using auth.contrib and its middleware?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 2, 2024, 11:32am UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/4 "2024-04-02T11:32:26Z")

</div>

> [@vinothrajs](#):
>
> Do we need to have one model per app or we can have one app with all models created …

See:

- [Largish project app structure](https://forum.djangoproject.com/t/largish-project-app-structure/7227)
- [categorizing models in own files instead of models.py - #6 by KenWhitesell](https://forum.djangoproject.com/t/categorizing-models-in-own-files-instead-of-models-py/26277/6)

> [@vinothrajs](#):
>
> Am planning to use htmx for ajax posts in all forms to make feel async and avoid page refresh,

I have personally fallen in love with HTMX and wouldn’t even think of starting a project now without it.

> [@vinothrajs](#):
>
> Can we use Raw sql only for large and complex

I don’t think I’ve ever used raw SQL for a Django model. We use the ORM exclusively.

> [@vinothrajs](#):
>
> also i tried Ajax form post, now how do we attach multiple models in a single form in Django

Do not confuse a Django Form with an HTML `<form>`. You can render multiple Django Forms within a single HTML `<form>` tag. See [Prefixes for forms](https://docs.djangoproject.com/en/5.0/ref/forms/api/#prefixes-for-forms).

> [@vinothrajs](#):
>
> I tried Abstractbaseuser but i would need the auth and authorize mechanism to handle dynamically from the table base don each URL so that i can handle that in middleware,

I’m sorry, I don’t understand what you’re trying to say here.  
(You do need to check authorization on each view. It doesn’t make sense in Django to do anything else.)

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [April 2, 2024, 7:27pm UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/5 "2024-04-02T19:27:41Z")

</div>

I don’t think “service layers” are a good idea in Django. The ORM is already your “service layer”.

Some classic posts from others on this subject:

> **[Against service layers in Django](https://www.b-list.org/weblog/2020/mar/16/no-service/)**
>
> \*This post now has \[a followup\](/weblog/2020/mar/23/still-no-service/).\* Recently I've seen posts and questions pop up in a few places about \[a …

> **[Evolution of a Django Repository pattern](https://lukeplant.me.uk/blog/posts/evolution-of-a-django-repository-pattern/)**
>
> First attempt - get product by primary key:
> class ProductRepository:
> def get\_by\_pk(self, pk):
> return Product.objects.get(pk=pk)
> 
> 
> ProductRepository is stateless, use static methods. Usage

---

<div class="post-metadata">

**Author:** ![vinothrajs](https://avatars.discourse-cdn.com/v4/letter/v/65b543/32.png) [@vinothrajs](https://forum.djangoproject.com/u/vinothrajs)\
**Post date:** [April 13, 2024, 5:29pm UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/6 "2024-04-13T17:29:02Z")

</div>

@KenWhitesell or @adamchainz thanks for the suggestion will use manager.

I also need help on the [Django custom Permission & Groups Table / Model](https://forum.djangoproject.com/t/django-custom-permission-groups-table-model/30129)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 13, 2024, 6:45pm UTC](https://forum.djangoproject.com/t/django-design-pattern-for-large-complex-web-app/29680/7 "2024-04-13T18:45:53Z")

</div>

Please do not tag individuals requesting assistance.

We are all volunteers here, answering questions as time, knowledge, and energy allows.
