Version 6 (modified by 17 years ago) ( diff ) | ,
---|
CookBook - Data Models - Splitting models across multiple files
It is not clear that this works anymore. Using the development version, revision 7013, I got
./manage.py sqlall example Error: App with label example could not be found. Are you sure your INSTALLED_APPS setting is correct?
To split models across multiple files, you can do the following (works at least with revision 2819):
The important thing to notice here is the app_label
attribute in the Meta
class. If that is omitted, a subsequent manage.py syncdb
run will not pick up these models.
NOTE: When #2982 gets applied you won't need the Meta
class anymore, so if this link: #2982 has a strike-through and you are running svn, you don't need the Meta
class.
apps/polls/models/__init__.py
:
from poll import Poll from choice import Choice
apps/polls/models/poll.py
:
from django.db import models class Poll(models.Model): question = models.CharField(maxlength=200) pub_date = models.DateTimeField('date published') class Meta: app_label = 'polls'
apps/polls/models/choice.py
:
from django.db import models from poll import Poll class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(maxlength=200) votes = models.IntegerField() class Meta: app_label = 'polls'
Ok so the example is a bit stupid but it should illustrate the point.
Providing initial sql data for models
If you provide initial sql data for you models (as described in Providing initial SQL data) you should notice that at least in Django 0.95 the path for sql files is different when this recipe is applied. In this case the path is:
<appname>/models/sql/<modelname>.sql