Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Handling request values with multiple values per key #86

Closed
wants to merge 1 commit into from

3 participants

@tmitchell

I discovered that the next and previous URI's don't get generated properly when you have multiple values per key.

For example: /api/v1/myresource?a=1&a=2

a retains only the value 2, not the list [1, 2] as is the case elsewhere in Django.

@georgemarshall

Wish I would of found this one earlier, this make my pull request a duplicate then.

#253

@issackelly issackelly closed this pull request from a commit
@issackelly issackelly Fixes Multiple values for a single key in our paginator. Fixes #86 --…
… Thanks tmitchell (Taylor Mitchell)

Conflicts:
	AUTHORS
797d4a4
@eyvoro eyvoro referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@hughes hughes referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@hughes hughes referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 1 deletion.
  1. +8 −1 tastypie/paginator.py
  2. +10 −0 tests/core/tests/paginator.py
View
9 tastypie/paginator.py
@@ -138,9 +138,16 @@ def _generate_uri(self, limit, offset):
request_params = self.request_data.copy()
request_params.update({'limit': limit, 'offset': offset})
+
+ try:
+ # QueryDict has a urlencode method that can handle multiple values for the same key
+ encoded_params = request_params.urlencode()
+ except AttributeError:
+ encoded_params = urlencode(request_params)
+
return '%s?%s' % (
self.resource_uri,
- urlencode(request_params)
+ encoded_params
)
def page(self):
View
10 tests/core/tests/paginator.py
@@ -5,6 +5,7 @@
from core.models import Note
from core.tests.resources import NoteResource
from django.db import reset_queries
+from django.http import QueryDict
class PaginatorTestCase(TestCase):
@@ -142,3 +143,12 @@ def test_regression_nonqueryset(self):
# differently.
page = paginator.page()
self.assertEqual(page['objects'], ['foo', 'bar'])
+
+ def test_multiple(self):
+ request = QueryDict('a=1&a=2')
+ paginator = Paginator(request, self.data_set, resource_uri='/api/v1/notes/', limit=2, offset=2)
+ meta = paginator.page()['meta']
+ self.assertEqual(meta['limit'], 2)
+ self.assertEqual(meta['offset'], 2)
+ self.assertEqual(meta['previous'], '/api/v1/notes/?a=1&a=2&limit=2&offset=0')
+ self.assertEqual(meta['next'], '/api/v1/notes/?a=1&a=2&limit=2&offset=4')
Something went wrong with that request. Please try again.