Django

Code

Ticket #4470 (closed: duplicate)

Opened 1 year ago

Last modified 2 weeks ago

Separate models.py in multiple files

Reported by: Sidi Mohamed EL AATIFI <elaatifi@gmail.com> Assigned to: nobody
Milestone: Component: Database wrapper
Version: SVN Keywords: models
Cc: brosner@gmail.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

I want to separate my models file (models.py) into multiple files : Product.py, Category.py, .... , which will be set up in the models directory. That´s 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 Product 
from Category import Category 
....

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.

Attachments

separates_models.diff (0.7 kB) - added by Sidi Mohamed EL AATIFI <elaatifi@gmail.com> on 06/03/07 13:19:01.
separate_models.7835.diff (0.6 kB) - added by semenov on 07/09/08 08:42:16.
New patch which works after [7777] models refactoring

Change History

06/03/07 13:19:01 changed by Sidi Mohamed EL AATIFI <elaatifi@gmail.com>

  • attachment separates_models.diff added.

06/20/07 23:26:52 changed by SmileyChris

  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests set to 1.
  • needs_docs changed.

This is actually a reasonable solution, no more hackish than it already is and saves the work-around step of having to provide app_label for all your split models.

I can't see a downside, but since it is quite core I'll move to design decision.

I guess some model tests could be written for split models to show it working still.

06/21/07 12:07:34 changed by eli.courtwright@gmail.com

I second this idea, because it also helps in cases where Django models are being used outside of a Django application (I do this frequently at my job).

06/21/07 14:32:55 changed by Brian Rosner <brosner@gmail.com>

  • cc set to brosner@gmail.com.

06/30/07 05:44:34 changed by cameron.royal@interzonegames.com

  • cc changed from brosner@gmail.com to cameron.royal@interzonegames.com.

+1 vote for this and would go so far as saying that everyone should do it. Means you can view what domain objects your application has at a glance through the file systems. Great for finding things :-)

06/30/07 05:45:57 changed by cameron.royal@interzonegames.com

  • cc changed from cameron.royal@interzonegames.com to brosner@gmail.com, cameron.royal@interzonegames.com.

track is dumb sometimes... (or maybe I should learn how to use it)

07/05/07 05:27:23 changed by mtredinnick

(In [5617]) Added a test that shows the problem in #4470. This fails only for the mysql_old backend. Refs #4470.

07/05/07 05:28:18 changed by mtredinnick

Ignore previous comment. Typo on my part -- should have referred to #4770.

07/10/07 20:50:12 changed by anonymous

  • cc changed from brosner@gmail.com, cameron.royal@interzonegames.com to brosner@gmail.com.

*emailed mined by spam bot*

08/27/07 12:16:08 changed by lee.a.connell@gmail.com

I would love to be able to split my models. Currently I am using models.py to import from my separated models.

# models.py

import book, author

class Book(book.Book):

pass

class Author(author.Author):

pass

(follow-up: ↓ 12 ) 09/16/07 14:40:14 changed by ubernostrum

  • status changed from new to closed.
  • resolution set to duplicate.

Since I'm almost 100% certain that the eventual solution to #3591 will solve this, I'm marking as a duplicate.

02/28/08 08:57:12 changed by guettli

I just read the statment (two comments above) from lee.a.connell@gmail.com

You don't need to subclass the classes you imported:

http://www.djangoproject.com/documentation/model-api/#models-across-files

from mysite.geography.models import ZipCode

class Restaurant(models.Model):
    # ...
    zip_code = models.ForeignKey(ZipCode)

(in reply to: ↑ 10 ) 06/25/08 10:59:18 changed by semenov

Replying to ubernostrum:

Since I'm almost 100% certain that the eventual solution to #3591 will solve this, I'm marking as a duplicate.

I can't see how #3591 would fix this issue.

#3591 is about giving applications custom names. This ticket is about being able to split models for a particular app into separate files. That doesn't have much to do with naming applications. Consider an unnamed application (in terms of #3591, "the old syntax app definition") and models split into models/*.py. With the latest trunk code ([7739], to be certain), that misleads ORM which starts looking for tables named models_<model>, not <app>_<model>.

I also can't understand very little interest from the public. Everyone is actually happy having all their models in a single file? Don't you agree that models.py becomes hardly maintanble even with half a dozen of non-trivial models?

By the way, Django developers themselves use the same approach in many places:

# Contents of django/newforms/__init__.py

from widgets import *
from fields import *
from forms import *
from models import *

# Nothing more! All entites gracefully split into subpackages.

Why not allow Django users to follow the same way for their models?

06/25/08 11:18:53 changed by ubernostrum

You can split up models, right now. It's just that the mechanism for doing so depends on app_label and therefore is more fragile than it needs to be. Thus, fixing app_label will, as a side effect, make this not be so fragile anymore, which means this really is a duplicate of #3591 because it refers to the same underlying issue.

07/09/08 08:41:28 changed by semenov

Okay, I see your point. Anyway, for people who just need to have the ability to split models right now and can't wait forever until the trunk is fixed, I am attaching the updated patch against [7835] (since as of [7777], the previous patch doesn't work).

07/09/08 08:42:16 changed by semenov

  • attachment separate_models.7835.diff added.

New patch which works after [7777] models refactoring


Add/Change #4470 (Separate models.py in multiple files)




Change Properties
Action