Code

Ticket #10227: patch.diff

File patch.diff, 2.9 KB (added by rvdrijst, 5 years ago)

Patch with fix and tests

Line 
1Index: django/db/models/fields/related.py
2===================================================================
3--- django/db/models/fields/related.py  (revision 9814)
4+++ django/db/models/fields/related.py  (working copy)
5@@ -185,7 +185,13 @@
6             return getattr(instance, self.cache_name)
7         except AttributeError:
8             params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
9-            rel_obj = self.related.model._default_manager.get(**params)
10+            try:
11+                rel_obj = self.related.model._default_manager.get(**params)
12+            except self.related.model.DoesNotExist:
13+                if self.related.field.null:
14+                    rel_obj = None
15+                else:
16+                    raise
17             setattr(instance, self.cache_name, rel_obj)
18             return rel_obj
19 
20Index: tests/modeltests/one_to_one_null/__init__.py
21===================================================================
22--- tests/modeltests/one_to_one_null/__init__.py        (revision 0)
23+++ tests/modeltests/one_to_one_null/__init__.py        (revision 0)
24@@ -0,0 +1 @@
25+
26
27Property changes on: tests/modeltests/one_to_one_null/__init__.py
28___________________________________________________________________
29Added: svn:keywords
30   + Id
31Added: svn:eol-style
32   + native
33
34Index: tests/modeltests/one_to_one_null/models.py
35===================================================================
36--- tests/modeltests/one_to_one_null/models.py  (revision 0)
37+++ tests/modeltests/one_to_one_null/models.py  (revision 0)
38@@ -0,0 +1,52 @@
39+"""
40+14. One-to-one relationships that can be null.
41+
42+To define a one-to-one relationship, use ``OneToOneField()`` with null=True.
43+
44+In this example, a ``Shop`` can optionally have a ``Place``.
45+E.g. webshops need not have a place, so it can be None/null.
46+"""
47+
48+from django.db import models
49+
50+class Place(models.Model):
51+    name = models.CharField(max_length=50)
52+    address = models.CharField(max_length=80)
53+
54+    def __unicode__(self):
55+        return u"%s the place" % self.name
56+
57+class Shop(models.Model):
58+    place = models.OneToOneField(Place, null=True)
59+    name = models.CharField(max_length=50)
60+    website = models.URLField()
61+   
62+    def __unicode__(self):
63+        return u"%s the shop" % self.name
64+
65+__test__ = {'API_TESTS':"""
66+# Create a place
67+>>> p1 = Place(name='Shop.com retail point', address='101 Somestr')
68+>>> p1.save()
69+
70+>>> print p1.shop
71+None
72+
73+>>> s1 = Shop(name='Shop', website='shop.com')
74+>>> s1.save()
75+>>> print s1.place
76+None
77+
78+>>> s1.place = p1
79+>>> s1.save()
80+
81+>>> s1.place
82+<Place: Shop.com retail point the place>
83+
84+# update p1 to reflect the new relation
85+>>> p1 = Place.objects.get(pk=p1.pk)
86+>>> p1.shop
87+<Shop: Shop the shop>
88+
89+
90+"""}
91
92Property changes on: tests/modeltests/one_to_one_null/models.py
93___________________________________________________________________
94Added: svn:keywords
95   + Id
96Added: svn:eol-style
97   + native
98
99