Ticket #7136: models.py

File models.py, 1.4 KB (added by admackin, 7 years ago)

model file with included doctest to replicate the issue

Line 
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...
23b1.subclassa: None
24b1.subclassb: None
25b1.subclassb: None
26b1.subclassa: None
27sA1.subclassa: SubclassA object
28sA1.subclassb: None
29sA1.subclassb: None
30sA1.subclassa: SubclassA object
31sB1.subclassa: None
32sB1.subclassb: SubclassB object
33sB1.subclassb: SubclassB object
34sB1.subclassa: None
35"""
36
37from django.db import models
38from django.core.exceptions import ObjectDoesNotExist
39
40
41class Base(models.Model):
42        ext_id = models.CharField(max_length=4)
43       
44class SubclassA(Base):
45        info = models.TextField()
46
47class SubclassB(Base):
48        comment = models.TextField()
49
50
51def testattr(inst, rel_name):
52        try:
53                return getattr(inst, rel_name)
54        except ObjectDoesNotExist:
55                return None
56
Back to Top