WBAR
WBAR

Reputation: 4984

Django 1.8: How use Model from different application?

My problem is I want to create something like common-application (this can be also a standard Python Module) with e.g.:

And also create application_1 and application_2 where models from common-application being used by this e.g.:

Really I don't know how models.py and apps.py should look like.. :/

Is it possible ?

PS. And If I want to write DATABASE_ROUTER to split this applications between two databases makes this problem really impossible to solve ?

Upvotes: 1

Views: 1684

Answers (3)

mulisa innocent
mulisa innocent

Reputation: 1

If you want to use a model from one app to another app For Example First App Model look like this This is the first modal that I want to use in the different app look like this

In the second model in a different app, it depends where you want to use that modal, for my case I use that model in view.py to retrieve information In this view import your model by writing from {yourappname}.models import {modelname}

Upvotes: 0

OrenD
OrenD

Reputation: 1741

You can implement the common models in common_application's models.py file as an abstract model, by adding the following to the model's class:

class Meta:
    abstract = True

Then, in other applications you can import your common models like so:

from common_application.models import Model_1, Model_2, Model_3

And then instantiate model classes that will derive from the abstract model class:

class Model_1a(Model_1):

More details on Model class inheritance can be found here

Upvotes: 2

VelikiiNehochuha
VelikiiNehochuha

Reputation: 4373

application1
    models (directory - package)
        __init__.py
        model1.py
        model2.py
        model3.py 

Into init.py files you could import all models from model*.py

from model1 import *
from model2 import *
from model3 import *

Also with models Meta you can set db_tablespace (https://docs.djangoproject.com/en/1.8/ref/models/options)

Upvotes: 0

Related Questions