| 1 | from django.db import models
|
|---|
| 2 |
|
|---|
| 3 | class Poll(models.Model):
|
|---|
| 4 | question = models.CharField(maxlength=200, db_column='custom_col_name')
|
|---|
| 5 |
|
|---|
| 6 | def __str__(self):
|
|---|
| 7 | return self.question
|
|---|
| 8 |
|
|---|
| 9 | class Admin:
|
|---|
| 10 | pass
|
|---|
| 11 |
|
|---|
| 12 | class Choice(models.Model):
|
|---|
| 13 | """
|
|---|
| 14 | # Create a couple of Poll instances
|
|---|
| 15 | >>> p1 = Poll.objects.create(question='What database do you use?')
|
|---|
| 16 | >>> p2 = Poll.objects.create(question='When will Django 1.0 be released?')
|
|---|
| 17 | >>> Poll.objects.all()
|
|---|
| 18 | [<Poll: What database do you use?>, <Poll: When will Django 1.0 be released?>]
|
|---|
| 19 |
|
|---|
| 20 | # Create Choice objects
|
|---|
| 21 | # First some related to the above Polls
|
|---|
| 22 | >>> c1 = Choice.objects.create(choice='Postgres', poll=p1)
|
|---|
| 23 | >>> c2 = Choice.objects.create(choice='Next week', poll=p2)
|
|---|
| 24 | >>> c3 = Choice.objects.create(choice='In 2007 Q2', poll=p2)
|
|---|
| 25 |
|
|---|
| 26 | # A Choice instance not related to any Poll (don't
|
|---|
| 27 | # create this the poll FK hasn't the null=True option)
|
|---|
| 28 | #>>> c4 = Choice.objects.create(choice='An orphan choice')
|
|---|
| 29 |
|
|---|
| 30 | #>>> Choice.objects.all()
|
|---|
| 31 | #[<Choice: An orphan choice>, <Choice: Postgres>, <Choice: Next week>, <Choice: In 2007 Q2>]
|
|---|
| 32 | >>> Choice.objects.all()
|
|---|
| 33 | [<Choice: Postgres>, <Choice: Next week>, <Choice: In 2007 Q2>]
|
|---|
| 34 |
|
|---|
| 35 | # THIS WILL FAIL
|
|---|
| 36 | # Test order_by using the old workaround using the related table
|
|---|
| 37 | # name + '.' + the field name that has a db_column option.
|
|---|
| 38 | >>> Choice.objects.order_by('ticket2210_poll.question')
|
|---|
| 39 | [<Choice: Postgres>, <Choice: Next week>, <Choice: In 2007 Q2>]
|
|---|
| 40 | >>> Choice.objects.order_by('-ticket2210_poll.question')
|
|---|
| 41 | [<Choice: Next week>, <Choice: In 2007 Q2>, <Choice: Postgres>]
|
|---|
| 42 | """
|
|---|
| 43 |
|
|---|
| 44 | #poll = models.ForeignKey(Poll, null=True)
|
|---|
| 45 | poll = models.ForeignKey(Poll)
|
|---|
| 46 | choice = models.CharField(maxlength=200)
|
|---|
| 47 |
|
|---|
| 48 | def __str__(self):
|
|---|
| 49 | return self.choice
|
|---|
| 50 |
|
|---|
| 51 | class Meta:
|
|---|
| 52 | ordering = ('poll',)
|
|---|
| 53 | #ordering = ('choice',)
|
|---|
| 54 |
|
|---|
| 55 | class Admin:
|
|---|
| 56 | list_display = ('choice', 'poll')
|
|---|