1 | """
|
---|
2 | >>> Base.objects.create(ext_id='b1')
|
---|
3 | <Base: Base object>
|
---|
4 | >>> SubclassA.objects.create(ext_id='sA1')
|
---|
5 | <SubclassA: SubclassA object>
|
---|
6 | >>> for b in Base.objects.all():
|
---|
7 | ... b.delete()
|
---|
8 | ...
|
---|
9 | >>> Base.objects.create(ext_id='b1')
|
---|
10 | <Base: Base object>
|
---|
11 | >>> SubclassA.objects.create(ext_id='sA1', info='foo')
|
---|
12 | <SubclassA: SubclassA object>
|
---|
13 | >>> SubclassB.objects.create(ext_id='sB1', comment='bar')
|
---|
14 | <SubclassB: SubclassB object>
|
---|
15 | >>> for ext_id in ('b1', 'sA1', 'sB1'):
|
---|
16 | ... inst = Base.objects.get(ext_id=ext_id)
|
---|
17 | ... for attrib in ('subclassa', 'subclassb'):
|
---|
18 | ... print "%s.%s: %s" % (ext_id, attrib, str(testattr(inst, attrib)))
|
---|
19 | ... inst = Base.objects.get(ext_id=ext_id)
|
---|
20 | ... for attrib in ('subclassb', 'subclassa'):
|
---|
21 | ... print "%s.%s: %s" % (ext_id, attrib, str(testattr(inst, attrib)))
|
---|
22 | ...
|
---|
23 | b1.subclassa: None
|
---|
24 | b1.subclassb: None
|
---|
25 | b1.subclassb: None
|
---|
26 | b1.subclassa: None
|
---|
27 | sA1.subclassa: SubclassA object
|
---|
28 | sA1.subclassb: None
|
---|
29 | sA1.subclassb: None
|
---|
30 | sA1.subclassa: SubclassA object
|
---|
31 | sB1.subclassa: None
|
---|
32 | sB1.subclassb: SubclassB object
|
---|
33 | sB1.subclassb: SubclassB object
|
---|
34 | sB1.subclassa: None
|
---|
35 | """
|
---|
36 |
|
---|
37 | from django.db import models
|
---|
38 | from django.core.exceptions import ObjectDoesNotExist
|
---|
39 |
|
---|
40 |
|
---|
41 | class Base(models.Model):
|
---|
42 | ext_id = models.CharField(max_length=4)
|
---|
43 |
|
---|
44 | class SubclassA(Base):
|
---|
45 | info = models.TextField()
|
---|
46 |
|
---|
47 | class SubclassB(Base):
|
---|
48 | comment = models.TextField()
|
---|
49 |
|
---|
50 |
|
---|
51 | def testattr(inst, rel_name):
|
---|
52 | try:
|
---|
53 | return getattr(inst, rel_name)
|
---|
54 | except ObjectDoesNotExist:
|
---|
55 | return None
|
---|
56 |
|
---|