Nameko/Django ORM integration

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.

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 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.

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:
    @rpc
    def method():
        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.

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 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.

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:
    @rpc
    def method():
        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.

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

It looks like
<python - Use only some parts of Django? - Stack Overflow; you
can use the Django ORM in a standalone fashion. Study
GitHub - nameko/nameko-sqlalchemy: SQLAlchemy dependency for nameko services
<Redirect Notice; 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):
    def setup(self):
        pass # whatever you have to do to initialise the Django ORM

    def get_dependency(self, worker_ctx):
        return models

    def worker_teardown(self, worker_ctx):
        pass # if you wanted to do auto-commit you'd trigger it here

class Service:
    name = "service"

    models = DjangoModels()
  
    def method(self):
         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
<Redirect Notice;
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.

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:
  
File
"/Users/menglongli/.virtualenvs/mid_backend/lib/python3.6/site-packages/django/conf/__init__.py",
line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File
"/Users/menglongli/.virtualenvs/mid_backend/lib/python3.6/importlib/__init__.py",
line 126, in import_module
    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:
    @rpc
    def method():
        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.

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.

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 :slight_smile:

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

2 Likes

Hello @jesusenlanet

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