Ticket #7780: models.py

File models.py, 1.2 KB (added by max@…, 16 years ago)

Example models and test code

Line 
1from django.db import models
2
3class Topping(models.Model):
4 name = models.CharField(max_length = 50)
5
6 def __unicode__(self):
7 return self.name
8
9
10class Pizza(models.Model):
11 name = models.CharField(max_length = 50)
12 toppings = models.ManyToManyField(Topping)
13
14 def __unicode__(self):
15 return self.name
16
17 class Meta:
18 ordering = ['name', 'toppings']
19
20## test with the following
21## create some toppings
22#tomato = Topping(name="Tomato")
23#tomato.save()
24#mozzarella = Topping(name="Mozzarella")
25#mozzarella.save()
26
27## create a pizza
28#pizza = Pizza(name="Margareta")
29#pizza.save()
30
31## add the toppings
32#pizza.toppings.add(tomato)
33#pizza.toppings.add(mozzarella)
34
35#p = Pizza.objects.get(pk=1)
36#Traceback (most recent call last):
37# File "<console>", line 1, in <module>
38# File "c:\python25\lib\site-packages\django\db\models\manager.py", line 82, in get return self.get_query_set().get(*args, **kwargs)
39# File "c:\python25\lib\site-packages\django\db\models\query.py", line 285, in get % (self.model._meta.object_name, num, kwargs))
40#MultipleObjectsReturned: get() returned more than one Pizza -- it returned 2! Lookup parameters were {'pk': 1}
41
Back to Top