mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Add a get_object method to the ModelMixin, and tests
This commit is contained in:
parent
26c1558e0f
commit
417eacb2ed
|
@ -557,6 +557,13 @@ class ModelMixin(object):
|
|||
|
||||
return all_kw_args
|
||||
|
||||
def get_object(self, *args, **kwargs):
|
||||
"""
|
||||
Get the instance object for read/update/delete requests.
|
||||
"""
|
||||
model = self.resource.model
|
||||
return model.objects.get(self.build_query(*args, **kwargs))
|
||||
|
||||
|
||||
class ReadModelMixin(ModelMixin):
|
||||
"""
|
||||
|
@ -566,7 +573,7 @@ class ReadModelMixin(ModelMixin):
|
|||
model = self.resource.model
|
||||
|
||||
try:
|
||||
self.model_instance = model.objects.get(self.build_query(*args, **kwargs))
|
||||
self.model_instance = self.get_object(*args, **kwargs)
|
||||
except model.DoesNotExist:
|
||||
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
@ -629,7 +636,7 @@ class UpdateModelMixin(ModelMixin):
|
|||
|
||||
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
|
||||
try:
|
||||
self.model_instance = model.objects.get(self.build_query(*args, **kwargs))
|
||||
self.model_instance = self.get_object(*args, **kwargs)
|
||||
|
||||
for (key, val) in self.CONTENT.items():
|
||||
setattr(self.model_instance, key, val)
|
||||
|
@ -647,7 +654,7 @@ class DeleteModelMixin(ModelMixin):
|
|||
model = self.resource.model
|
||||
|
||||
try:
|
||||
instance = model.objects.get(self.build_query(*args, **kwargs))
|
||||
instance = self.get_object(*args, **kwargs)
|
||||
except model.DoesNotExist:
|
||||
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
|
||||
|
||||
|
@ -689,7 +696,7 @@ class ListModelMixin(ModelMixin):
|
|||
|
||||
def get_queryset(self):
|
||||
model = self.resource.model
|
||||
return model.objects.all() if self.queryset is None else self.queryset
|
||||
return model.objects.all() if self.queryset is None else self.queryset
|
||||
|
||||
|
||||
########## Pagination Mixins ##########
|
||||
|
|
|
@ -4,14 +4,47 @@ from django.utils import simplejson as json
|
|||
from djangorestframework import status
|
||||
from djangorestframework.compat import RequestFactory
|
||||
from django.contrib.auth.models import Group, User
|
||||
from djangorestframework.mixins import CreateModelMixin, PaginatorMixin
|
||||
from djangorestframework.mixins import CreateModelMixin, PaginatorMixin, ReadModelMixin
|
||||
from djangorestframework.resources import ModelResource
|
||||
from djangorestframework.response import Response
|
||||
from djangorestframework.response import Response, ErrorResponse
|
||||
from djangorestframework.tests.models import CustomUser
|
||||
from djangorestframework.tests.testcases import TestModelsTestCase
|
||||
from djangorestframework.views import View
|
||||
|
||||
|
||||
class TestModelRead(TestModelsTestCase):
|
||||
"""Tests on ReadModelMixin"""
|
||||
|
||||
def setUp(self):
|
||||
super(TestModelRead, self).setUp()
|
||||
self.req = RequestFactory()
|
||||
|
||||
def test_read(self):
|
||||
Group.objects.create(name='other group')
|
||||
group = Group.objects.create(name='my group')
|
||||
|
||||
class GroupResource(ModelResource):
|
||||
model = Group
|
||||
|
||||
request = self.req.get('/groups')
|
||||
mixin = ReadModelMixin()
|
||||
mixin.resource = GroupResource
|
||||
|
||||
response = mixin.get(request, group.id)
|
||||
self.assertEquals(group.name, response.name)
|
||||
|
||||
def test_read_404(self):
|
||||
class GroupResource(ModelResource):
|
||||
model = Group
|
||||
|
||||
request = self.req.get('/groups')
|
||||
mixin = ReadModelMixin()
|
||||
mixin.resource = GroupResource
|
||||
|
||||
with self.assertRaises(ErrorResponse):
|
||||
response = mixin.get(request, 12345)
|
||||
|
||||
|
||||
class TestModelCreation(TestModelsTestCase):
|
||||
"""Tests on CreateModelMixin"""
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user