Skip to content

Loading…

Change the "format" GET parameter to be overridden in django settings. #703

Open
wants to merge 2 commits into from

2 participants

@craigjackson

"format' is a very easy field name to conflict with. Instead of forcing the user to use a different field name to resolve conflicts, I would like to allow the dev to be able to overwrite the format parameter name in settings.

My ultimate goal is to make the "format" parameter to default to something less likely to conflict (e.g. "_format"). That will be a different request, and may be a backwards-compatibility issue. So if we allow the format to be overridden, then I can just set it to "_format" on my project without a forked version of tastypie.

@joshbohde

Thanks for the pull request. Before it could be merged, it would need tests, per our contribution guidelines.

I'm -1 on making this a setting. A Resource.Meta option would allow future APIs to use this, while not breaking existing APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 22, 2012
  1. Change default "format" to be a django setting.

    Craig Jackson committed
  2. Adjust the return message for the format parameter.

    Craig Jackson committed
Showing with 21 additions and 4 deletions.
  1. +14 −0 docs/settings.rst
  2. +2 −1 tastypie/serializers.py
  3. +5 −3 tastypie/utils/mime.py
View
14 docs/settings.rst
@@ -98,3 +98,17 @@ An example::
TASTYPIE_DATETIME_FORMATTING = 'rfc-2822'
Defaults to ``iso-8601``.
+
+
+``TASTYPIE_FORMAT_PARAMETER``
+=============================
+
+**Optional**
+
+This setting controls the format parameter name checked in API requests.
+
+An example::
+
+ TASTYPIE_FORMAT_PARAMETER = "_format"
+
+Defaults to ``format``.
View
3 tastypie/serializers.py
@@ -432,7 +432,8 @@ def to_html(self, data, options=None):
implemented.
"""
options = options or {}
- return 'Sorry, not implemented yet. Please append "?format=json" to your URL.'
+ format_parameter = getattr(settings, 'TASTYPIE_FORMAT_PARAMETER', 'format')
+ return 'Sorry, not implemented yet. Please append "?' + format_parameter + '=json" to your URL.'
def from_html(self, content):
"""
View
8 tastypie/utils/mime.py
@@ -1,5 +1,6 @@
import mimeparse
+from django.conf import settings
def determine_format(request, serializer, default_format='application/json'):
"""
@@ -15,9 +16,10 @@ def determine_format(request, serializer, default_format='application/json'):
to ``application/json`` if not provided).
"""
# First, check if they forced the format.
- if request.GET.get('format'):
- if request.GET['format'] in serializer.formats:
- return serializer.get_mime_for_format(request.GET['format'])
+ format_parameter = getattr(settings, 'TASTYPIE_FORMAT_PARAMETER', 'format')
+ if request.GET.get(format_parameter):
+ if request.GET[format_parameter] in serializer.formats:
+ return serializer.get_mime_for_format(request.GET[format_parameter])
# If callback parameter is present, use JSONP.
if request.GET.has_key('callback'):
Something went wrong with that request. Please try again.