mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 13:04:03 +03:00
Fix up ModelResource issues
This commit is contained in:
parent
5a59f339c1
commit
5921e5c84e
|
@ -15,8 +15,10 @@ from djangorestframework.breadcrumbs import get_breadcrumbs
|
||||||
from djangorestframework.description import get_name, get_description
|
from djangorestframework.description import get_name, get_description
|
||||||
from djangorestframework import status
|
from djangorestframework import status
|
||||||
|
|
||||||
from decimal import Decimal
|
from urllib import quote_plus
|
||||||
import string
|
import string
|
||||||
|
import re
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
# TODO: Rename verbose to something more appropriate
|
# TODO: Rename verbose to something more appropriate
|
||||||
# TODO: Maybe None could be handled more cleanly. It'd be nice if it was handled by default,
|
# TODO: Maybe None could be handled more cleanly. It'd be nice if it was handled by default,
|
||||||
|
|
|
@ -408,6 +408,9 @@ class ModelResource(Resource):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class InstanceModelResource(ModelResource):
|
||||||
|
http_method_names = ['get', 'put', 'delete', 'head', 'options', 'trace', 'patch'] # Bit of a hack, these - needs fixing.
|
||||||
|
|
||||||
class RootModelResource(ModelResource):
|
class RootModelResource(ModelResource):
|
||||||
"""A Resource which provides default operations for list and create."""
|
"""A Resource which provides default operations for list and create."""
|
||||||
queryset = None
|
queryset = None
|
||||||
|
@ -416,7 +419,7 @@ class RootModelResource(ModelResource):
|
||||||
queryset = self.queryset if self.queryset else self.model.objects.all()
|
queryset = self.queryset if self.queryset else self.model.objects.all()
|
||||||
return queryset.filter(**kwargs)
|
return queryset.filter(**kwargs)
|
||||||
|
|
||||||
put = delete = None
|
http_method_names = ['get', 'post', 'head', 'options', 'trace', 'patch']
|
||||||
|
|
||||||
class QueryModelResource(ModelResource):
|
class QueryModelResource(ModelResource):
|
||||||
"""Resource with default operations for list.
|
"""Resource with default operations for list.
|
||||||
|
@ -428,4 +431,4 @@ class QueryModelResource(ModelResource):
|
||||||
queryset = self.queryset if self.queryset else self.model.objects.all()
|
queryset = self.queryset if self.queryset else self.model.objects.all()
|
||||||
return queryset.filer(**kwargs)
|
return queryset.filer(**kwargs)
|
||||||
|
|
||||||
post = put = delete = None
|
http_method_names = ['get', 'head', 'options', 'trace', 'patch']
|
||||||
|
|
|
@ -54,7 +54,7 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def allowed_methods(self):
|
def allowed_methods(self):
|
||||||
return [method.upper() for method in self.http_method_names if getattr(self, method, None)]
|
return [method.upper() for method in self.http_method_names if hasattr(self, method)]
|
||||||
|
|
||||||
def http_method_not_allowed(self, request, *args, **kwargs):
|
def http_method_not_allowed(self, request, *args, **kwargs):
|
||||||
"""Return an HTTP 405 error if an operation is called which does not have a handler method."""
|
"""Return an HTTP 405 error if an operation is called which does not have a handler method."""
|
||||||
|
@ -97,9 +97,6 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
# Get the appropriate handler method
|
# Get the appropriate handler method
|
||||||
if self.method.lower() in self.http_method_names:
|
if self.method.lower() in self.http_method_names:
|
||||||
handler = getattr(self, self.method.lower(), self.http_method_not_allowed)
|
handler = getattr(self, self.method.lower(), self.http_method_not_allowed)
|
||||||
# If a previously defined method has been disabled
|
|
||||||
if handler is None:
|
|
||||||
handler = self.http_method_not_allowed
|
|
||||||
else:
|
else:
|
||||||
handler = self.http_method_not_allowed
|
handler = self.http_method_not_allowed
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from djangorestframework.modelresource import ModelResource, RootModelResource
|
from djangorestframework.modelresource import InstanceModelResource, RootModelResource
|
||||||
|
|
||||||
from blogpost import models
|
from blogpost import models
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ class BlogPosts(RootModelResource):
|
||||||
model = models.BlogPost
|
model = models.BlogPost
|
||||||
fields = BLOG_POST_FIELDS
|
fields = BLOG_POST_FIELDS
|
||||||
|
|
||||||
class BlogPostInstance(ModelResource):
|
class BlogPostInstance(InstanceModelResource):
|
||||||
"""A resource which represents a single blog post."""
|
"""A resource which represents a single blog post."""
|
||||||
model = models.BlogPost
|
model = models.BlogPost
|
||||||
fields = BLOG_POST_FIELDS
|
fields = BLOG_POST_FIELDS
|
||||||
|
@ -21,7 +21,7 @@ class Comments(RootModelResource):
|
||||||
model = models.Comment
|
model = models.Comment
|
||||||
fields = COMMENT_FIELDS
|
fields = COMMENT_FIELDS
|
||||||
|
|
||||||
class CommentInstance(ModelResource):
|
class CommentInstance(InstanceModelResource):
|
||||||
"""A resource which represents a single comment."""
|
"""A resource which represents a single comment."""
|
||||||
model = models.Comment
|
model = models.Comment
|
||||||
fields = COMMENT_FIELDS
|
fields = COMMENT_FIELDS
|
||||||
|
|
Loading…
Reference in New Issue
Block a user