Skip to content
Browse files

Fixed ``detail_uri_name`` not being used when generating URIs.

  • Loading branch information...
1 parent fd9f058 commit 630cfda5d16f67e353f84b27a36bdad9c54a4b87 @dbtsai dbtsai committed with toastdriven Jun 11, 2012
Showing with 51 additions and 13 deletions.
  1. +2 −2 tastypie/resources.py
  2. +49 −11 tests/basic/tests/resources.py
View
4 tastypie/resources.py
@@ -2069,9 +2069,9 @@ def detail_uri_kwargs(self, bundle_or_obj):
kwargs = {}
if isinstance(bundle_or_obj, Bundle):
- kwargs[self._meta.detail_uri_name] = bundle_or_obj.obj.pk
+ kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj.obj, self._meta.detail_uri_name)
else:
- kwargs[self._meta.detail_uri_name] = bundle_or_obj.id
+ kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj, self._meta.detail_uri_name)
return kwargs
View
60 tests/basic/tests/resources.py
@@ -2,26 +2,27 @@
from django.test import TestCase
from tastypie.fields import ToOneField, ToManyField
from tastypie.resources import ModelResource
+from basic.api.resources import SlugBasedNoteResource
from basic.models import Note, AnnotatedNote
class InvalidLazyUserResource(ModelResource):
notes = ToManyField('basic.api.resources.FooResource', 'notes')
-
+
class Meta:
queryset = User.objects.all()
class NoPathLazyUserResource(ModelResource):
notes = ToManyField('FooResource', 'notes')
-
+
class Meta:
queryset = User.objects.all()
class LazyUserResource(ModelResource):
notes = ToManyField('basic.tests.resources.NoteResource', 'notes')
-
+
class Meta:
queryset = User.objects.all()
api_name = 'foo'
@@ -39,7 +40,7 @@ class Meta:
class NoteWithAnnotationsResource(ModelResource):
annotated = ToOneField(AnnotatedNoteResource, 'annotated', null=True)
-
+
class Meta:
queryset = Note.objects.all()
@@ -50,32 +51,32 @@ def test_init(self):
self.assertEqual(len(resource_1.fields), 8)
self.assertNotEqual(resource_1._meta.queryset, None)
self.assertEqual(resource_1._meta.resource_name, 'note')
-
+
# TextFields should have ``default=''`` to match Django's behavior,
# even though that's not what is on the field proper.
self.assertEqual(resource_1.fields['content'].default, '')
-
+
def test_lazy_relations(self):
ilur = InvalidLazyUserResource()
nplur = NoPathLazyUserResource()
lur = LazyUserResource()
-
+
self.assertEqual(ilur.notes.to, 'basic.api.resources.FooResource')
self.assertEqual(nplur.notes.to, 'FooResource')
self.assertEqual(lur.notes.to, 'basic.tests.resources.NoteResource')
-
+
try:
ilur.notes.to_class()
self.fail("to_class on InvalidLazyUserResource should fail!")
except ImportError:
pass
-
+
try:
nplur.notes.to_class()
self.fail("to_class on NoPathLazyUserResource should fail!")
except ImportError:
pass
-
+
to_class = lur.notes.to_class()
self.assertTrue(isinstance(to_class, NoteResource))
# This is important, as without passing on the ``api_name``, URL
@@ -90,7 +91,44 @@ def test_one_to_one_regression(self):
# Make sure bits don't completely blow up if the related model
# is gone.
n1 = Note.objects.get(pk=1)
-
+
resource_1 = NoteWithAnnotationsResource()
n1_bundle = resource_1.build_bundle(obj=n1)
dehydrated = resource_1.full_dehydrate(n1_bundle)
+
+
+class DetailURIKwargsResourceTestCase(TestCase):
+ def test_correct_detail_uri_model(self):
+ n1 = Note.objects.get(pk=1)
+
+ resource = NoteWithAnnotationsResource()
+ self.assertEqual(resource.detail_uri_kwargs(n1), {
+ 'pk': 1,
+ })
+
+ def test_correct_detail_uri_bundle(self):
+ n1 = Note.objects.get(pk=1)
+
+ resource = NoteWithAnnotationsResource()
+ n1_bundle = resource.build_bundle(obj=n1)
+ self.assertEqual(resource.detail_uri_kwargs(n1_bundle), {
+ 'pk': 1,
+ })
+
+ def test_correct_slug_detail_uri_model(self):
+ n1 = Note.objects.get(pk=1)
+
+ resource = SlugBasedNoteResource()
+ self.assertEqual(resource.detail_uri_kwargs(n1), {
+ 'slug': 'first-post',
+ })
+
+ def test_correct_slug_detail_uri_bundle(self):
+ n1 = Note.objects.get(pk=1)
+
+ resource = SlugBasedNoteResource()
+ n1_bundle = resource.build_bundle(obj=n1)
+ self.assertEqual(resource.detail_uri_kwargs(n1_bundle), {
+ 'slug': 'first-post',
+ })
+

0 comments on commit 630cfda

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