diff options
| author | Mjumbe Wawatu Poe | 2012-01-20 13:05:44 -0500 |
|---|---|---|
| committer | Mjumbe Wawatu Poe | 2012-01-20 13:05:44 -0500 |
| commit | 417eacb2eda46feec0e8fb7fac5eb3131ab996f8 (patch) | |
| tree | e05e60b8fb0288b395875d6ed636078def0d76c0 /djangorestframework/mixins.py | |
| parent | 26c1558e0f10ebf6549d51e29255ada2e8825521 (diff) | |
| download | django-rest-framework-417eacb2eda46feec0e8fb7fac5eb3131ab996f8.tar.bz2 | |
Add a get_object method to the ModelMixin, and tests
Diffstat (limited to 'djangorestframework/mixins.py')
| -rw-r--r-- | djangorestframework/mixins.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index c7f7b10c..66ab9071 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -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 ########## |
