About local apps communication

18 views
Skip to first unread message

durir...@gmail.com

unread,
Aug 16, 2015, 6:33:11 AM8/16/15
to Django users
I had searched in the web about local communication between apps, but I didn't find anything (or, at least, didn't catch it) about calling an app from another. Let's say I've this:
http://localhost:8000/app1/resources/user?id=123
http://localhost:8000/app2/list_users

Each app (by the way, first time doing a really RESTfull service, maybe I'm wrong with the style) have their models. app1 have a table for user, and app2 needs to communicate with it to list all the users (gived or not a QUERYSTRING for filtering). From app2.views, how could I get that data in a Django way?

James Schneider

unread,
Aug 16, 2015, 6:54:56 AM8/16/15
to django...@googlegroups.com

If both apps are running on the same host (or more concisely, both apps can access the database for app1), why not just add a database router to app2 and have it pull the information directly?

https://docs.djangoproject.com/en/1.8/topics/db/multi-db/

Another option would be to have logic in app2 that can query against app1 via typical HTTP REST requests (perhaps triggered by an HTTP call to a view in app2 if the info needs to be pulled on demand), using app2 as if it were a regular client for app1.

A third option if the information is synced on a regular basis would be to write Django management commands and run them on a schedule via cron to pull the data from app1 to app2.

These options are in order of preference, as direct DB access is the fastest and easiest to manage, but may be negatively affected if the schema changes in app1.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/34bdde3e-6422-49e0-9285-9113ed0abf7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen J. Butler

unread,
Aug 16, 2015, 6:55:29 AM8/16/15
to django...@googlegroups.com
These are all in the same project, right? In your app2 just do:

from app1.models import MyUserModel

Or maybe factor out the logic of app1/resources/user to a function and do from app2:

from app1.utils import get_user_logic

There's no special communication between Django apps required. All the code from every app in your project (INSTALLED_APPS) is loaded into/available from your current interpreter instance.

--

James Schneider

unread,
Aug 16, 2015, 6:59:33 AM8/16/15
to django...@googlegroups.com

Ah yes, my answer only applies if you have two separate instances of Django running with separate databases. That was my assumption since the full database is available to all apps within a single Django instance as Stephen mentioned.

-James

durir...@gmail.com

unread,
Aug 16, 2015, 9:28:59 AM8/16/15
to Django users
Thanks for the responses. Now I'll try to explain ahead:

If both apps are running on the same host (or more concisely, both apps can access the database for app1), why not just add a database router to app2 and have it pull the information directly?
https://docs.djangoproject.com/en/1.8/topics/db/multi-db/
Another option would be to have logic in app2 that can query against app1 via typical HTTP REST requests (perhaps triggered by an HTTP call to a view in app2 if the info needs to be pulled on demand), using app2 as if it were a regular client for app1.
A third option if the information is synced on a regular basis would be to write Django management commands and run them on a schedule via cron to pull the data from app1 to app2.
These options are in order of preference, as direct DB access is the fastest and easiest to manage, but may be negatively affected if the schema changes in app1.
 
[...]
 
Ah yes, my answer only applies if you have two separate instances of Django running with separate databases. That was my assumption since the full database is available to all apps within a single Django instance as Stephen mentioned. 
 
 
-James
 
I think that the optimal option would be the second one (doing a request), because of the background of the apps maybe the client would only use the user app, for example. And no, it'll be just one instance of Django for the apps.


These are all in the same project, right? In your app2 just do:
from app1.models import MyUserModel
Or maybe factor out the logic of app1/resources/user to a function and do from app2:
from app1.utils import get_user_logic
There's no special communication between Django apps required. All the code from every app in your project (INSTALLED_APPS) is loaded into/available from your current interpreter instance. 

That's the point, they are in the same project, and supossed to be (because it'll be a package with more than one app). I though that doing something with INSTALLED_APPS would work, but didn't know about that import. And about the factoring function, yeah, it is supossed to be REST, so a utils.py or rest.py file should do the work.


Thanks again both.
Reply all
Reply to author
Forward
0 new messages