# Nameko/Django ORM integration

**URL:** <https://discourse.nameko.io/t/nameko-django-orm-integration/166>\
**Category:** googlegroup\
**Created:** [February 3, 2017, 8:48am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166 "2017-02-03T08:48:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trikster1911](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@Trikster1911](https://discourse.nameko.io/u/Trikster1911)\
**Post date:** [February 3, 2017, 8:48am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/1 "2017-02-03T08:48:49Z")

</div>

Hello guys, thank you for your library and your work. Here is a question:  
while trying to integrate Nameko into our project I've stumbled across the  
necessity to integrate it with Django ORM, don't you know what are the best  
ways to accomplish it, exactly how to correctly wrap Django ORM as a  
dependency? I've attempted initializing django application in custom  
DependecyProvider and returning required models module in get\_dependency()  
method but I feel it's not the best solution.  
Regards, Anders.

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [February 3, 2017, 11:00am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/2 "2017-02-03T11:00:24Z")

</div>

That is an interesting question. I don't know much about the Django ORM, or  
how easy it is to use outside of Django. Can you explain a bit more about  
what you're trying to achieve?

You might also want to take a look at  
[GitHub - nameko/nameko-sqlalchemy: SQLAlchemy dependency for nameko services](https://github.com/onefinestay/nameko-sqlalchemy) for an example of how to  
do it with sqlalchemy.

> **···**
>
> On Friday, February 3, 2017 at 8:48:49 AM UTC, trikst...@gmail.com wrote:
> 
> > Hello guys, thank you for your library and your work. Here is a question:  
> > while trying to integrate Nameko into our project I've stumbled across the  
> > necessity to integrate it with Django ORM, don't you know what are the best  
> > ways to accomplish it, exactly how to correctly wrap Django ORM as a  
> > dependency? I've attempted initializing django application in custom  
> > DependecyProvider and returning required models module in get\_dependency()  
> > method but I feel it's not the best solution.  
> > Regards, Anders.

---

<div class="post-metadata">

**Author:** ![Marco\_Lavagnino](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@Marco\_Lavagnino](https://discourse.nameko.io/u/Marco_Lavagnino)\
**Post date:** [June 14, 2017, 11:57pm UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/3 "2017-06-14T23:57:12Z")

</div>

Just do what you would do if you wanted to create a standalone django  
script:

import django  
import os  
from django.conf import settings  
from nameko import rpc

os.environ.setdefault("DJANGO\_SETTINGS\_MODULE", "project.settings")  
django.setup()

from someapp.models import SomeModel

class Service:  
&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def method():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SomeModel.objects.all().count()

> **···**
>
> El viernes, 3 de febrero de 2017, 5:48:49 (UTC-3), trikst...@gmail.com escribió:
> 
> > Hello guys, thank you for your library and your work. Here is a question:  
> > while trying to integrate Nameko into our project I've stumbled across the  
> > necessity to integrate it with Django ORM, don't you know what are the best  
> > ways to accomplish it, exactly how to correctly wrap Django ORM as a  
> > dependency? I've attempted initializing django application in custom  
> > DependecyProvider and returning required models module in get\_dependency()  
> > method but I feel it's not the best solution.  
> > Regards, Anders.

---

<div class="post-metadata">

**Author:** ![Trikster1911](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@Trikster1911](https://discourse.nameko.io/u/Trikster1911)\
**Post date:** [February 3, 2017, 11:16am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/4 "2017-02-03T11:16:01Z")

</div>

Well, we are currently migrating our semi-monolitic web application from  
Django to a set of nameko-based microservices and in order to simplify our  
life we want to use part of our django models with nameko services.

> **···**
>
> пятница, 3 февраля 2017 г., 14:00:25 UTC+3 пользователь Matt Yule-Bennett написал:
> 
> > That is an interesting question. I don't know much about the Django ORM,  
> > or how easy it is to use outside of Django. Can you explain a bit more  
> > about what you're trying to achieve?
> > 
> > You might also want to take a look at  
> > [GitHub - nameko/nameko-sqlalchemy: SQLAlchemy dependency for nameko services](https://github.com/onefinestay/nameko-sqlalchemy) for an example of how to  
> > do it with sqlalchemy.
> > 
> > On Friday, February 3, 2017 at 8:48:49 AM UTC, trikst...@gmail.com wrote:
> > 
> > > Hello guys, thank you for your library and your work. Here is a question:  
> > > while trying to integrate Nameko into our project I've stumbled across the  
> > > necessity to integrate it with Django ORM, don't you know what are the best  
> > > ways to accomplish it, exactly how to correctly wrap Django ORM as a  
> > > dependency? I've attempted initializing django application in custom  
> > > DependecyProvider and returning required models module in get\_dependency()  
> > > method but I feel it's not the best solution.  
> > > Regards, Anders.

---

<div class="post-metadata">

**Author:** ![Marco\_Lavagnino](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@Marco\_Lavagnino](https://discourse.nameko.io/u/Marco_Lavagnino)\
**Post date:** [June 21, 2017, 1:30pm UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/5 "2017-06-21T13:30:23Z")

</div>

Update: this won't work. It opens multiple connections to the db and  
doesn't automatically close them (as it would do if you were using django  
views).

> **···**
>
> El miércoles, 14 de junio de 2017, 20:57:12 (UTC-3), marco.l...@gmail.com escribió:
> 
> > Just do what you would do if you wanted to create a standalone django  
> > script:
> > 
> > import django  
> > import os  
> > from django.conf import settings  
> > from nameko import rpc
> > 
> > os.environ.setdefault("DJANGO\_SETTINGS\_MODULE", "project.settings")  
> > django.setup()
> > 
> > from someapp.models import SomeModel
> > 
> > class Service:  
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def method():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SomeModel.objects.all().count()
> > 
> > El viernes, 3 de febrero de 2017, 5:48:49 (UTC-3), trikst...@gmail.com \> escribió:
> > 
> > > Hello guys, thank you for your library and your work. Here is a question:  
> > > while trying to integrate Nameko into our project I've stumbled across the  
> > > necessity to integrate it with Django ORM, don't you know what are the best  
> > > ways to accomplish it, exactly how to correctly wrap Django ORM as a  
> > > dependency? I've attempted initializing django application in custom  
> > > DependecyProvider and returning required models module in get\_dependency()  
> > > method but I feel it's not the best solution.  
> > > Regards, Anders.

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [February 3, 2017, 11:45am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/6 "2017-02-03T11:45:45Z")

</div>

OK, so the main objective is re-using existing code?

It looks like  
\<[python - Use only some parts of Django? - Stack Overflow](http://stackoverflow.com/questions/302651/use-only-some-parts-of-django&gt); you  
can use the Django ORM in a standalone fashion. Study  
[GitHub - nameko/nameko-sqlalchemy: SQLAlchemy dependency for nameko services](https://github.com/onefinestay/nameko-sqlalchemy)  
\<[Redirect Notice](https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fonefinestay%2Fnameko-sqlalchemy&sa=D&sntz=1&usg=AFQjCNErSczcIE0uOaTaGt9qUTwnAssA4Q&gt); for  
an example of how to write a DependencyProvider. The main thing a  
DependencyProvider needs to be aware of is the worker lifecycle and  
concurrency. You can see in nameko-sqlalchemy that sessions returned by  
get\_dependency are stashed so they can be closed when the worker is torn  
down (which is after the service method has executed).

It might be that the Django ORM is thread-safe and you don't need to worry  
about that stuff at all, in which case you can maybe just import the models  
and use them directly in your service code. To keep with the nameko  
principle of injecting dependencies though, I would try to access the  
models from a DependencyProvider. That way the database can still easily be  
mocked out during tests.

Assuming that Django ORM \*is\* thread-safe, it might look like this:

form app import models

class DjangoModels(DependencyProvider):  
&nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass # whatever you have to do to initialise the Django ORM

&nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return models

&nbsp;&nbsp;&nbsp;&nbsp;def worker\_teardown(self, worker\_ctx):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass # if you wanted to do auto-commit you'd trigger it here

class Service:  
&nbsp;&nbsp;&nbsp;&nbsp;name = "service"

&nbsp;&nbsp;&nbsp;&nbsp;models = DjangoModels()  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def method(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo1 = self.models.FooModel.objects.filter(id=1)

> **···**
>
> On Friday, February 3, 2017 at 11:16:01 AM UTC, trikst...@gmail.com wrote:
> 
> > Well, we are currently migrating our semi-monolitic web application from  
> > Django to a set of nameko-based microservices and in order to simplify our  
> > life we want to use part of our django models with nameko services.
> > 
> > пятница, 3 февраля 2017 г., 14:00:25 UTC+3 пользователь Matt Yule-Bennett \> написал:
> > 
> > > That is an interesting question. I don't know much about the Django ORM,  
> > > or how easy it is to use outside of Django. Can you explain a bit more  
> > > about what you're trying to achieve?
> > > 
> > > You might also want to take a look at  
> > > [GitHub - nameko/nameko-sqlalchemy: SQLAlchemy dependency for nameko services](https://github.com/onefinestay/nameko-sqlalchemy)  
> > > \<[Redirect Notice](https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fonefinestay%2Fnameko-sqlalchemy&sa=D&sntz=1&usg=AFQjCNErSczcIE0uOaTaGt9qUTwnAssA4Q&gt);  
> > > for an example of how to do it with sqlalchemy.
> > > 
> > > On Friday, February 3, 2017 at 8:48:49 AM UTC, trikst...@gmail.com wrote:
> > > 
> > > > Hello guys, thank you for your library and your work. Here is a  
> > > > question: while trying to integrate Nameko into our project I've stumbled  
> > > > across the necessity to integrate it with Django ORM, don't you know what  
> > > > are the best ways to accomplish it, exactly how to correctly wrap Django  
> > > > ORM as a dependency? I've attempted initializing django application in  
> > > > custom DependecyProvider and returning required models module in  
> > > > get\_dependency() method but I feel it's not the best solution.  
> > > > Regards, Anders.

---

<div class="post-metadata">

**Author:** ![Albert\_Menglongli](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Albert\_Menglongli](https://discourse.nameko.io/u/Albert_Menglongli)\
**Post date:** [January 12, 2018, 7:36am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/7 "2018-01-12T07:36:35Z")

</div>

I got Django importing module error, when running codes like this using  
command nameko run xxxx, my directory is like project/xxxx, and the error  
looks like:  
&nbsp;&nbsp;  
File  
"/Users/menglongli/.virtualenvs/mid\_backend/lib/python3.6/site-packages/django/conf/\_\_init\_\_.py",  
line 106, in \_\_init\_\_  
&nbsp;&nbsp;&nbsp;&nbsp;mod = importlib.import\_module(self.SETTINGS\_MODULE)  
&nbsp;&nbsp;File  
"/Users/menglongli/.virtualenvs/mid\_backend/lib/python3.6/importlib/\_\_init\_\_.py",  
line 126, in import\_module  
&nbsp;&nbsp;&nbsp;&nbsp;return \_bootstrap.\_gcd\_import(name[level:], package, level)  
ModuleNotFoundError: No module named 'project'

No idea about how you guys fix this?

在 2017年6月21日星期三 UTC+8下午9:30:23，marco.l...@gmail.com写道：

> **···**
>
> > Update: this won't work. It opens multiple connections to the db and  
> > doesn't automatically close them (as it would do if you were using django  
> > views).
> > 
> > El miércoles, 14 de junio de 2017, 20:57:12 (UTC-3), marco.l...@gmail.com \> escribió:
> > 
> > > Just do what you would do if you wanted to create a standalone django  
> > > script:
> > > 
> > > import django  
> > > import os  
> > > from django.conf import settings  
> > > from nameko import rpc
> > > 
> > > os.environ.setdefault("DJANGO\_SETTINGS\_MODULE", "project.settings")  
> > > django.setup()
> > > 
> > > from someapp.models import SomeModel
> > > 
> > > class Service:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def method():  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SomeModel.objects.all().count()
> > > 
> > > El viernes, 3 de febrero de 2017, 5:48:49 (UTC-3), trikst...@gmail.com \>\> escribió:
> > > 
> > > > Hello guys, thank you for your library and your work. Here is a  
> > > > question: while trying to integrate Nameko into our project I've stumbled  
> > > > across the necessity to integrate it with Django ORM, don't you know what  
> > > > are the best ways to accomplish it, exactly how to correctly wrap Django  
> > > > ORM as a dependency? I've attempted initializing django application in  
> > > > custom DependecyProvider and returning required models module in  
> > > > get\_dependency() method but I feel it's not the best solution.  
> > > > Regards, Anders.

---

<div class="post-metadata">

**Author:** ![Trikster1911](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@Trikster1911](https://discourse.nameko.io/u/Trikster1911)\
**Post date:** [February 3, 2017, 11:59am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/8 "2017-02-03T11:59:11Z")

</div>

Thank you for your reply, that's just the way I've implemented, except for  
I've made some kind of Repository layer on top of Django models providing  
methods for our domain.

---

<div class="post-metadata">

**Author:** ![Johan\_Frisell](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@Johan\_Frisell](https://discourse.nameko.io/u/Johan_Frisell)\
**Post date:** [May 25, 2017, 9:13am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/9 "2017-05-25T09:13:30Z")

</div>

Just to help future visitors of this thread, interested in integrating  
Django with Nameko. Django is thread-safe. I will myself try to implement  
this in a project, I'll get back with the result 🙂

---

<div class="post-metadata">

**Author:** ![jesusenlanet](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/jesusenlanet/32/51_2.png) [@jesusenlanet](https://discourse.nameko.io/u/jesusenlanet)\
**Post date:** [April 8, 2019, 9:51am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/10 "2019-04-08T09:51:28Z")

</div>

Hi guys,

Some months ago I created a package called django-nameko-standalone.  
It tries to solve this problem, please check it and feel free to participate:  
[https://github.com/jesusenlanet/django-nameko-standalone](https://github.com/jesusenlanet/django-nameko-standalone)

---

<div class="post-metadata">

**Author:** ![checor](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/checor/32/156_2.png) [@checor](https://discourse.nameko.io/u/checor)\
**Post date:** [March 29, 2022, 3:03am UTC](https://discourse.nameko.io/t/nameko-django-orm-integration/166/11 "2022-03-29T03:03:47Z")

</div>

Hello @jesusenlanet

Is there a reason the project was discontinued? Seems like a neat tool to transition from django project to nameko microservices!
