Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7551 -- Made GFK allow None init argument.

Thanks SamBull for the report.
  • Loading branch information...
commit 3918eeb9fde03a2ad1941bce022615fff8eae34d 1 parent 763ac8b
Bouke Haarsma authored October 15, 2013 timgraham committed October 16, 2013
8  django/contrib/contenttypes/generic.py
@@ -59,8 +59,12 @@ def instance_pre_init(self, signal, sender, args, kwargs, **_kwargs):
59 59
         """
60 60
         if self.name in kwargs:
61 61
             value = kwargs.pop(self.name)
62  
-            kwargs[self.ct_field] = self.get_content_type(obj=value)
63  
-            kwargs[self.fk_field] = value._get_pk_val()
  62
+            if value is not None:
  63
+                kwargs[self.ct_field] = self.get_content_type(obj=value)
  64
+                kwargs[self.fk_field] = value._get_pk_val()
  65
+            else:
  66
+                kwargs[self.ct_field] = None
  67
+                kwargs[self.fk_field] = None
64 68
 
65 69
     def get_content_type(self, obj=None, id=None, using=None):
66 70
         if obj is not None:
7  tests/generic_relations/models.py
@@ -137,3 +137,10 @@ class ConcreteRelatedModel(models.Model):
137 137
 class ProxyRelatedModel(ConcreteRelatedModel):
138 138
     class Meta:
139 139
         proxy = True
  140
+
  141
+
  142
+# To test fix for #7551
  143
+class AllowsNullGFK(models.Model):
  144
+    content_type = models.ForeignKey(ContentType, null=True)
  145
+    object_id = models.PositiveIntegerField(null=True)
  146
+    content_object = generic.GenericForeignKey()
17  tests/generic_relations/tests.py
@@ -4,11 +4,12 @@
4 4
 from django.contrib.contenttypes.generic import generic_inlineformset_factory
5 5
 from django.contrib.contenttypes.models import ContentType
6 6
 from django.test import TestCase
  7
+from django.utils import six
7 8
 
8 9
 from .models import (TaggedItem, ValuableTaggedItem, Comparison, Animal,
9 10
                      Vegetable, Mineral, Gecko, Rock, ManualPK,
10 11
                      ForProxyModelModel, ForConcreteModelModel,
11  
-                     ProxyRelatedModel, ConcreteRelatedModel)
  12
+                     ProxyRelatedModel, ConcreteRelatedModel, AllowsNullGFK)
12 13
 
13 14
 
14 15
 class GenericRelationsTests(TestCase):
@@ -440,3 +441,17 @@ def test_generic_relation_set(self):
440 441
         newrel.bases = [base]
441 442
         newrel = ConcreteRelatedModel.objects.get(pk=newrel.pk)
442 443
         self.assertEqual(base, newrel.bases.get())
  444
+
  445
+
  446
+class TestInitWithNoneArgument(TestCase):
  447
+    def test_none_not_allowed(self):
  448
+        # TaggedItem requires a content_type, initializing with None should
  449
+        # raise a ValueError.
  450
+        with six.assertRaisesRegex(self, ValueError,
  451
+          'Cannot assign None: "TaggedItem.content_type" does not allow null values'):
  452
+            TaggedItem(content_object=None)
  453
+
  454
+    def test_none_allowed(self):
  455
+        # AllowsNullGFK doesn't require a content_type, so None argument should
  456
+        # also be allowed.
  457
+        AllowsNullGFK(content_object=None)

0 notes on commit 3918eeb

Please sign in to comment.
Something went wrong with that request. Please try again.