开发者

How can I use one django app ( with one db ) in two projects ? ( Using my app as a service )

开发者 https://www.devze.com 2023-03-19 10:08 出处:网络
The Problem -> Suppose you have a django app called \"blog\", that you want to use in \"www.cool.com\" and \"www.hot.com\" which would essentially be two different django projects, ( except for the \"

The Problem ->

Suppose you have a django app called "blog", that you want to use in "www.cool.com" and "www.hot.com" which would essentially be two different django projects, ( except for the "blog" app ofcourse ).

I want to this "blog" app to be like a service, which can be used by any 3rd party site.

How would you do about doing it ?

One of the sol开发者_开发问答utions I thought was to host this "blog" app on another 3rd domain and handle the read/writes through an API but this would get quite cumbersome, I was thinking maybe there is a better way. Something I'm missing perhaps. Thank you.

Somebody correct me if I'm wrong but what I think I'm trying to do is use my Django App as a service.

A little more detail because I feel I may have not been entirely clear,

Excerpts from the comments below ->

I want the codebase of the django app to remain in one place, but it should be usable by "n" number of projects.

can I add a django app from the cloud into my INSTALLED APPS of my django settings.py somehow?


The question is very, very hard to follow.

Two django sites which share a common app is trivial. It's done all the time.

You're simply sharing the app code among the two sites.

Use subversion to keep the master copy of the blog app.

Checkout working copies for both sites.

I want to this "blog" app to be like a service, which can be used by any 3rd party site. I think I'm trying to do is use my Django App as a service.

This is trivial also. Use Piston to create a RESTful web service around your blog app.

handle the read/writes through an API but this would get quite cumbersome.

Not really. RESTful web services are quite trivial. Use httplib to be a client of a RESTful web service.


I think simplest solution will be:
django muliple databases + lets say, PostgreSQL DB allowing crossdomain access.
Create router for your blog app and use any db you want.

UPD:

Somewhere

# class for routing db operations for  some blog myapp
class MyAppRouter(object):
    """A router to control all database operations on models in
    the myapp application"""

    def db_for_read(self, model, **hints):
        "Point all operations on myapp models to 'other'"
        if model._meta.app_label == 'myapp':
            return 'other'
        return None

    def db_for_write(self, model, **hints):
        "Point all operations on myapp models to 'other'"
        if model._meta.app_label == 'myapp':
            return 'other'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        "Allow any relation if a model in myapp is involved"
        if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp':
            return True
        return None

    def allow_syncdb(self, db, model):
        "Make sure the myapp app only appears on the 'other' db"
        if db == 'other':
            return model._meta.app_label == 'myapp'
        elif model._meta.app_label == 'myapp':
            return False
        return None

in settings.py

DATABASES = {
'default': {
     ...
     # Project DB
           }
# Blog DB
'blog': { 
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': 'test',
    'USER': 'postgres',
    'PASSWORD': 'admin',
    'HOST': '111.111.111.111',
    'PORT': '5432',
    }
}

...
DATABASE_ROUTERS = ['path.to.MyAppRouter', 'path.to.MasterSlaveRouter']
0

精彩评论

暂无评论...
验证码 换一张
取 消