Ticket #2210: models2.py

File models2.py, 1.9 KB (added by ramiro <rm0 _at_ gmx.net>, 17 years ago)
Line 
1from django.db import models
2
3class 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
12class 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 # Test ordeirng but this time using the patch attached to ticket #2076
36 # and the new notation for specifying ordering by a related model field.
37 # There is no need to use table names here
38 >>> Choice.objects.order_by('poll__question')
39 [<Choice: Postgres>, <Choice: Next week>, <Choice: In 2007 Q2>]
40 >>> Choice.objects.order_by('-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')
Back to Top