Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 630cfda5d16f67e353f84b27a36bdad9c54a4b87 1 parent fd9f058
DB Tsai dbtsai authored toastdriven committed

Showing 2 changed files with 51 additions and 13 deletions. Show diff stats Hide diff stats

  1. +2 2 tastypie/resources.py
  2. +49 11 tests/basic/tests/resources.py
4 tastypie/resources.py
@@ -2069,9 +2069,9 @@ def detail_uri_kwargs(self, bundle_or_obj):
2069 2069 kwargs = {}
2070 2070
2071 2071 if isinstance(bundle_or_obj, Bundle):
2072   - kwargs[self._meta.detail_uri_name] = bundle_or_obj.obj.pk
  2072 + kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj.obj, self._meta.detail_uri_name)
2073 2073 else:
2074   - kwargs[self._meta.detail_uri_name] = bundle_or_obj.id
  2074 + kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj, self._meta.detail_uri_name)
2075 2075
2076 2076 return kwargs
2077 2077
60 tests/basic/tests/resources.py
@@ -2,26 +2,27 @@
2 2 from django.test import TestCase
3 3 from tastypie.fields import ToOneField, ToManyField
4 4 from tastypie.resources import ModelResource
  5 +from basic.api.resources import SlugBasedNoteResource
5 6 from basic.models import Note, AnnotatedNote
6 7
7 8
8 9 class InvalidLazyUserResource(ModelResource):
9 10 notes = ToManyField('basic.api.resources.FooResource', 'notes')
10   -
  11 +
11 12 class Meta:
12 13 queryset = User.objects.all()
13 14
14 15
15 16 class NoPathLazyUserResource(ModelResource):
16 17 notes = ToManyField('FooResource', 'notes')
17   -
  18 +
18 19 class Meta:
19 20 queryset = User.objects.all()
20 21
21 22
22 23 class LazyUserResource(ModelResource):
23 24 notes = ToManyField('basic.tests.resources.NoteResource', 'notes')
24   -
  25 +
25 26 class Meta:
26 27 queryset = User.objects.all()
27 28 api_name = 'foo'
@@ -39,7 +40,7 @@ class Meta:
39 40
40 41 class NoteWithAnnotationsResource(ModelResource):
41 42 annotated = ToOneField(AnnotatedNoteResource, 'annotated', null=True)
42   -
  43 +
43 44 class Meta:
44 45 queryset = Note.objects.all()
45 46
@@ -50,32 +51,32 @@ def test_init(self):
50 51 self.assertEqual(len(resource_1.fields), 8)
51 52 self.assertNotEqual(resource_1._meta.queryset, None)
52 53 self.assertEqual(resource_1._meta.resource_name, 'note')
53   -
  54 +
54 55 # TextFields should have ``default=''`` to match Django's behavior,
55 56 # even though that's not what is on the field proper.
56 57 self.assertEqual(resource_1.fields['content'].default, '')
57   -
  58 +
58 59 def test_lazy_relations(self):
59 60 ilur = InvalidLazyUserResource()
60 61 nplur = NoPathLazyUserResource()
61 62 lur = LazyUserResource()
62   -
  63 +
63 64 self.assertEqual(ilur.notes.to, 'basic.api.resources.FooResource')
64 65 self.assertEqual(nplur.notes.to, 'FooResource')
65 66 self.assertEqual(lur.notes.to, 'basic.tests.resources.NoteResource')
66   -
  67 +
67 68 try:
68 69 ilur.notes.to_class()
69 70 self.fail("to_class on InvalidLazyUserResource should fail!")
70 71 except ImportError:
71 72 pass
72   -
  73 +
73 74 try:
74 75 nplur.notes.to_class()
75 76 self.fail("to_class on NoPathLazyUserResource should fail!")
76 77 except ImportError:
77 78 pass
78   -
  79 +
79 80 to_class = lur.notes.to_class()
80 81 self.assertTrue(isinstance(to_class, NoteResource))
81 82 # This is important, as without passing on the ``api_name``, URL
@@ -90,7 +91,44 @@ def test_one_to_one_regression(self):
90 91 # Make sure bits don't completely blow up if the related model
91 92 # is gone.
92 93 n1 = Note.objects.get(pk=1)
93   -
  94 +
94 95 resource_1 = NoteWithAnnotationsResource()
95 96 n1_bundle = resource_1.build_bundle(obj=n1)
96 97 dehydrated = resource_1.full_dehydrate(n1_bundle)
  98 +
  99 +
  100 +class DetailURIKwargsResourceTestCase(TestCase):
  101 + def test_correct_detail_uri_model(self):
  102 + n1 = Note.objects.get(pk=1)
  103 +
  104 + resource = NoteWithAnnotationsResource()
  105 + self.assertEqual(resource.detail_uri_kwargs(n1), {
  106 + 'pk': 1,
  107 + })
  108 +
  109 + def test_correct_detail_uri_bundle(self):
  110 + n1 = Note.objects.get(pk=1)
  111 +
  112 + resource = NoteWithAnnotationsResource()
  113 + n1_bundle = resource.build_bundle(obj=n1)
  114 + self.assertEqual(resource.detail_uri_kwargs(n1_bundle), {
  115 + 'pk': 1,
  116 + })
  117 +
  118 + def test_correct_slug_detail_uri_model(self):
  119 + n1 = Note.objects.get(pk=1)
  120 +
  121 + resource = SlugBasedNoteResource()
  122 + self.assertEqual(resource.detail_uri_kwargs(n1), {
  123 + 'slug': 'first-post',
  124 + })
  125 +
  126 + def test_correct_slug_detail_uri_bundle(self):
  127 + n1 = Note.objects.get(pk=1)
  128 +
  129 + resource = SlugBasedNoteResource()
  130 + n1_bundle = resource.build_bundle(obj=n1)
  131 + self.assertEqual(resource.detail_uri_kwargs(n1_bundle), {
  132 + 'slug': 'first-post',
  133 + })
  134 +

0 comments on commit 630cfda

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