1 | from django.db import models
|
---|
2 |
|
---|
3 | class Vocabulary(models.Model):
|
---|
4 | root = models.ForeignKey('Term', null=True, blank=True, related_name='root_of')
|
---|
5 |
|
---|
6 | def save(self):
|
---|
7 | """When a vocabulary is saved and it has no root term, a new root
|
---|
8 | term is created. This happens only the first time a vocabulary is
|
---|
9 | saved.
|
---|
10 | """
|
---|
11 | super(Vocabulary, self).save()
|
---|
12 |
|
---|
13 | if not self.root:
|
---|
14 | self.root = Term.objects.create(vocabulary=self)
|
---|
15 | super(Vocabulary, self).save()
|
---|
16 |
|
---|
17 | class Term(models.Model):
|
---|
18 | vocabulary = models.ForeignKey(Vocabulary)
|
---|
19 |
|
---|
20 | def save(self):
|
---|
21 | super(Term, self).save()
|
---|
22 |
|
---|
23 | # As soon as self.parent is referenced here, the unittest fails.
|
---|
24 | # It does not matter if the self.parent is printed, used or changed.
|
---|
25 | # Any of the following will make the unittest fail!
|
---|
26 | #print "term.vocabulary:", self.vocabulary
|
---|
27 | self.vocabulary
|
---|
28 |
|
---|