| 1 | = CookBook - [wiki:CookBookDataModels Data Models] - Splitting models across multiple files = |
| 2 | |
| 3 | To split models across multiple files, you can do the following (works at least with revision 2819): |
| 4 | |
| 5 | {{{apps/polls/models/__init__.py}}}: |
| 6 | {{{ |
| 7 | from poll import Poll |
| 8 | from choice import Choice |
| 9 | }}} |
| 10 | |
| 11 | {{{apps/polls/models/poll.py}}}: |
| 12 | {{{ |
| 13 | from django.db import models |
| 14 | |
| 15 | class Poll(models.Model): |
| 16 | question = models.CharField(maxlength=200) |
| 17 | pub_date = models.DateTimeField('date published') |
| 18 | |
| 19 | class Meta: |
| 20 | app_label = 'polls' |
| 21 | }}} |
| 22 | |
| 23 | {{{apps/polls/models/choice.py}}}: |
| 24 | {{{ |
| 25 | from django.db import models |
| 26 | from poll import Poll |
| 27 | |
| 28 | class Choice(models.Model): |
| 29 | poll = models.ForeignKey(Poll) |
| 30 | choice = models.CharField(maxlength=200) |
| 31 | votes = models.IntegerField() |
| 32 | |
| 33 | class Meta: |
| 34 | app_label = 'polls' |
| 35 | }}} |
| 36 | |
| 37 | Ok so the example is a bit stupid but it should illustrate the point. |