Naturally, model classes contain methods for working with the model. If I have a book model with the book.get_noun_count() method, where it belongs, I donโt want to write " get_noun_count(book) ", unless the method essentially belongs to another package. (Maybe, for example, if I have a package for accessing the Amazon API with " get_amazon_product_id(book) ".)
I shrunk when Django's documentation suggested placing models in a single file, and I took a few minutes from the start to figure out how to split it into the correct subpackage.
site/models/__init__.py site/models/book.py
__init__.py looks like this:
from .book import Book
so I can still write "from the book import sites .models."
For versions prior to Django 1.7, only the following is required: https://code.djangoproject.com/ticket/3591
The only trick is that you need to explicitly install each model application due to an error in Django: it is assumed that the application name is the third to the last entry in the model path. "site.models.Book" leads to a "site", which is correct; "site.models.book.Book" makes us think that the name of the application is "models". This is a pretty nasty hack on the part of Django; he should probably look for a list of installed applications to match the prefix.
class Book(models.Model): class Meta: app_label = "site"
Perhaps you could use a base class or a metaclass to generalize it, but I have not worked on this yet.
Glenn Maynard Jul 21 '09 at 18:02 2009-07-21 18:02
source share