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 ?
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.
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() .
import sys
sys.path.append('/root/project1')
from appx.models import Foox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With