Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 django projects, importing one model from one to another

My setup is Django 1.2 running via mod_wsgi under Debian Lenny.

I have a such structure:

/root/
    project1/appx
                 models.py
    project2/appy
                 models.py
                 management/
                           commands/
                                   mycommand.py

Now I want to import Foox model from project1 to project2.

What would be the easiest solution WITHOUT moving the whole project dir to Python_path ? I especially need a solution without using mod_wsgi because I will be importing this model from project1 to custom manage.py command called "mycommand" in project2 ?

like image 728
Hellnar Avatar asked Jul 30 '10 07:07

Hellnar


People also ask

How import all models in Django?

To do this, create a directory called /«project»/«app_name»/models , inside it put __init__.py (to declare it as a module) and then create your files inside there. You then need to import your file contents into the module in __init__.py . You should read about Python modules to understand this.

How do you create an instance of a model from another model in Django?

To create a new instance of a model, instantiate it like any other Python class: class Model (**kwargs) The keyword arguments are the names of the fields you've defined on your model. Note that instantiating a model in no way touches your database; for that, you need to save() .


1 Answers

import sys
sys.path.append('/root/project1')
from appx.models import Foox
like image 87
Daniel Roseman Avatar answered Oct 21 '22 04:10

Daniel Roseman