Separate models.py in multiple files

295 views
Skip to first unread message
Message has been deleted

EL AATIFI Sidi Mohamed

unread,
Jun 3, 2007, 11:26:34 AM6/3/07
to django-d...@googlegroups.com
Hi,
Currently I develop a large website using Django, and I have in the
project some applications with a very big models file,
and I want to separate it in multiple files : Product.py, Category.py,
.... , which will be set up in the models directory.That愀 to say:

+ myapp/
+ models/
|- __init__.py
|- Product.py
|- Category.py
|- Media.py
|- ...

Since models is a module, we have to add the file __init__.py:
from Product import Post
from Category import Author
....

When doing, there are some actions from manage.py related to models that
will not work.
It can be fixed, by associating models explicitly to the application :

class Product(models.Model):
...
class Meta:
app_label = 'myapp'

The app_label is deducted from the name of model module
(django/db/models/base.py, ):
new_class._meta.app_label = model_module.__name__.split('.')[-2]
So when the model module name is myproject.myapp.models.product, the
app_label deducted is models and not myapp, and this is
why manage.py syndb doesnt work for example.

I suggest to change it:

module_name = model_module.__name__.split('.')
new_class._meta.app_label = module_name[module_name.index('models')-1]

it works perfectly in the both cases.


separates_models.diff

Russell Keith-Magee

unread,
Jun 3, 2007, 9:29:10 PM6/3/07
to django-d...@googlegroups.com
On 6/3/07, EL AATIFI Sidi Mohamed <elaa...@gmail.com> wrote:

> I suggest to change it:
>
> module_name = model_module.__name__.split('.')
> new_class._meta.app_label = module_name[module_name.index('models')-1]
>
> it works perfectly in the both cases.

Hi,

Thanks for the suggestion. Could you please submit this as a ticket on
our ticket tracker. This message will get lost on the mailing list;
putting it on the ticket tracker means we won't forget to look at
this.

Yours,
Russ Magee

Reply all
Reply to author
Forward
0 new messages