Skip to content

Commit

Permalink
Permit changing existing value on a ToOneField to None. (Closes djang…
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanHayes committed Feb 17, 2016
1 parent 779fe7d commit 73425c3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/release_notes/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ copied to the release notes for the next release.
Bugfixes
--------

* list of issues/PRs
* Permit changing existing value on a ToOneField to None. (Closes #1449)
12 changes: 10 additions & 2 deletions tastypie/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
except (ImproperlyConfigured, ImportError):
GeometryField = None
from django.db.models.constants import LOOKUP_SEP
try:
from django.db.models.fields.related import\
SingleRelatedObjectDescriptor as ReverseOneToOneDescriptor
except ImportError:
from django.db.models.fields.related_descriptors import\
ReverseOneToOneDescriptor
from django.db.models.sql.constants import QUERY_TERMS
from django.http import HttpResponse, HttpResponseNotFound, Http404
from django.utils import six
Expand Down Expand Up @@ -944,10 +950,12 @@ def full_hydrate(self, bundle):
setattr(bundle.obj, field_object.attribute, value.obj)
except (ValueError, ObjectDoesNotExist):
bundle.related_objects_to_save[field_object.attribute] = value.obj
elif field_object.null:
if not isinstance(getattr(bundle.obj.__class__, field_object.attribute, None), ReverseOneToOneDescriptor):
# only update if not a reverse one to one field
setattr(bundle.obj, field_object.attribute, value)
elif field_object.blank:
continue
elif field_object.null:
setattr(bundle.obj, field_object.attribute, value)

return bundle

Expand Down
31 changes: 31 additions & 0 deletions tests/core/tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,37 @@ def test_full_hydrate(self):

self.assertEquals(hydrated.obj.name, "Daniel")

def test_full_hydrate__can_put_null_to_clear_related_value(self):
class RelatedBasicResource(BasicResource):
parent = fields.ToOneField(BasicResource, 'parent', null=True, blank=True)
basic = RelatedBasicResource()
basic_bundle_1 = Bundle(data={
'name': 'Daniel',
'view_count': 6,
'date_joined': None,
'parent': None
})
basic_bundle_1.obj = Mock()
basic_bundle_1.obj.date_joined = aware_datetime(2010, 2, 15, 12, 0, 0)
basic_bundle_1.obj.parent = Mock()

self.assertEqual(basic_bundle_1.data['date_joined'], None)
self.assertEqual(basic_bundle_1.data['parent'], None)
self.assertNotEqual(basic_bundle_1.obj.date_joined, None)
self.assertNotEqual(basic_bundle_1.obj.parent, None)

# Now load up the data.
hydrated = basic.full_hydrate(basic_bundle_1)

self.assertEqual(hydrated.data['name'], 'Daniel')
self.assertEqual(hydrated.data['view_count'], 6)
self.assertEqual(hydrated.data['date_joined'], None)
self.assertEqual(hydrated.data['parent'], None)
self.assertEqual(hydrated.obj.name, 'Daniel')
self.assertEqual(hydrated.obj.view_count, 6)
self.assertEqual(hydrated.obj.date_joined, None)
self.assertEqual(hydrated.obj.parent, None)

def test_obj_get_list(self):
basic = BasicResource()
bundle = Bundle()
Expand Down

0 comments on commit 73425c3

Please sign in to comment.