CookBook - Data Models - Splitting models across multiple files

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 db_table and app_label attributes in the Meta class. If those are omitted, a subsequent syncdb run will not pick up these models.

NOTE: When #3591 gets applied you won't need the Meta class anymore, so if this link: #3591 has a strike-through and you are running svn, you don't need the Meta class.

This has been verified to work without the Meta class db_table, in the current main branch.


from poll import Poll
from choice import Choice


from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    class Meta:
        db_table = 'poll'
        app_label= 'polls'


from django.db import models

class Choice(models.Model):
    poll = models.ForeignKey('Poll')
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

    class Meta:
        db_table = 'choice'
        app_label = 'polls'

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:


Last modified 11 years ago Last modified on 12/10/11 14:23:41
Back to Top