Permalink
Browse files

Make ToOneField respect readonly during save_related

  • Loading branch information...
1 parent b248c21 commit 77ce0328799c5c3848f9949dfe314aa726bbd227 @dstufft dstufft committed Sep 4, 2012
Showing with 37 additions and 0 deletions.
  1. +3 −0 tastypie/resources.py
  2. +34 −0 tests/core/tests/resources.py
View
@@ -2077,6 +2077,9 @@ def save_related(self, bundle):
if not field_object.attribute:
continue
+ if field_object.readonly:
+ continue
+
if field_object.blank and not bundle.data.has_key(field_name):
continue
@@ -3024,6 +3024,40 @@ def test_readonly_full_hydrate(self):
hydrated_2 = rornr.full_hydrate(hbundle_2)
self.assertEqual(hydrated_2.obj.author.username, 'johndoe')
+ def test_readonly_save_related(self):
+ rornr = ReadOnlyRelatedNoteResource()
+ note = Note.objects.get(pk=1)
+ dbundle = Bundle(obj=note)
+
+ # Make sure the field is there on read.
+ dehydrated = rornr.full_dehydrate(dbundle)
+ self.assertTrue('author' in dehydrated.data)
+
+ # Fetch the bundle
+ hbundle = Bundle(obj=note, data={
+ 'name': 'Daniel',
+ 'view_count': 6,
+ 'date_joined': aware_datetime(2010, 2, 15, 12, 0, 0),
+ 'author': '/api/v1/users/2/',
+ })
+ hydrated = rornr.full_hydrate(hbundle)
+
+ # Get the related object.
+ related_obj = getattr(hydrated.obj, "author")
+
+ # Monkey Patch save to raise an exception
+ def fake_save(*args, **kwargs):
+ raise Exception("save() called in a readonly field")
+
+ _real_save = related_obj.save
+
+ try:
+ related_obj.save = fake_save
+
+ rornr.save_related(hydrated)
+ finally:
+ related_obj.save = _real_save
+
class BasicAuthResourceTestCase(TestCase):
fixtures = ['note_testdata.json']

0 comments on commit 77ce032

Please sign in to comment.