Share models among applications and split them in multiple files

254 views
Skip to first unread message

Alex Rades

unread,
Jul 4, 2008, 5:08:02 AM7/4/08
to django...@googlegroups.com
Hi,
I have a project with 3 application which could (should) share their models. Morover, I'd like to put models into a separate directory, splitted into multiple files. Something like:

myproject/
   models/
      box.py
      firmware.py
      component.py     
   myapp1/
   myapp2/
   myapp3/

Is it possible to organize the project in this way?

Thank you

Malcolm Tredinnick

unread,
Jul 4, 2008, 5:26:35 AM7/4/08
to django...@googlegroups.com

Not really. Each model "belongs to" ("is defined in") a single
application. All that means (and exactly what it means) is that the
model's "app_name" is that application's name and there are a few cases
(e.g. default database table) where we use <app_name> + <model_name> to
generate identifiers.

However, you might be approaching this the wrong way. Any application
can import models from any other application. So if you want to split
out certain pieces of business logic or presentation logic into other
applications -- which isn't necessarily a bad thing -- then put those
items into one app and they can import the models from the models'
originating app.

Now, as for putting the models into separate files: that's possible and
it has to be under the models/ directory in one of your apps. The only
thing to know here is that you must then set the "app_name" attribute on
the inner Meta class for each model to be the name of the app directory.
For example, with this structure:

myapp1/
models/
component.py
...

any model in component.py will have to say something like

class Component(models.Model):
...
class Meta:
app_name = 'myapp1'

So "app_name" is just the application's directory name (*just* that
directory name, nothing with dots in it or anything like that).

Hope that gives you some ideas.

Regards,
Malcolm

>
> Thank you
>
> >

Alex Rades

unread,
Jul 4, 2008, 6:22:33 AM7/4/08
to Malcolm Tredinnick, django...@googlegroups.com

Hi Malcom,
My 3 applications have all the same level of relation with the models so I'd rather prefer not to privilege one of them as the main app/models owner. Anyway, I could create an application whose only function is models container, I can live with this :)

As for the models splitting, I've tried the approach you suggested, but it doesn't work yet. My project structure is the following

|-- models_app
|   |-- models
|   |   |-- __init__.py
|   |   |-- backend.py
|   |   |-- box.py
|   |   `-- release.py
|   `-- __init__.py
|-- __init__.py
|-- app.db
|-- manage.py
|-- settings.py
`-- urls.py

So for now there is only this models_app which should contain the models. When I try to run syncdb, models under models/ are not imported. Is there any magic to do into models_app/models/__init__.py ?

Adi J. Sieker

unread,
Jul 4, 2008, 6:33:41 AM7/4/08
to django...@googlegroups.com
On Fri, 04 Jul 2008 12:22:33 +0200, Alex Rades <aler...@gmail.com> wrote:

> So for now there is only this models_app which should contain the
> models. When I try to run syncdb, models under models/ are not imported.
> Is there any magic to do into models_app/models/__init__.py ?

There is no magic in the models. You just need to make sure you added
models_app
to your INSTALLED_APPS directive in the settings.py.
syncdb only install models from installed apps.

Regards
adi

Malcolm Tredinnick

unread,
Jul 4, 2008, 6:33:28 AM7/4/08
to django...@googlegroups.com

On Fri, 2008-07-04 at 12:22 +0200, Alex Rades wrote:
[...]

> So for now there is only this models_app which should contain the
> models. When I try to run syncdb, models under models/ are not
> imported. Is there any magic to do into
> models_app/models/__init__.py ?

You need to import them into the models/ namespace, because Django only
looks in <app_name>.models for any model definitions. So your
__init__.py will be a bunch of imports from your subfiles (with
judicious use of __all__ in the subfiles, it's just going to be a list
of "from component import *" sort of lines).

Regards,
Malcolm

Alex Rades

unread,
Jul 4, 2008, 7:03:12 AM7/4/08
to Malcolm Tredinnick, django...@googlegroups.com, Adi J. Sieker
Ok I've got it working. I had to import models in models_app/models/__init__.py as Malcom suggested. I've used app_label instead of app_name in the Meta classes to make it work, is this ok?
Reply all
Reply to author
Forward
0 new messages