aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/mixins.py
diff options
context:
space:
mode:
authorFernando Zunino2011-07-01 03:32:04 -0300
committerFernando Zunino2011-07-01 03:32:04 -0300
commit60cd5363ab1343260c4ed8322d2d26064d56c3d3 (patch)
treec3eaa03a47bc84eb2f89324cf30e57cfd0fa3e3f /djangorestframework/mixins.py
parent8bafa01abd9bcc104aa413f09ab9505abb3526a2 (diff)
downloaddjango-rest-framework-60cd5363ab1343260c4ed8322d2d26064d56c3d3.tar.bz2
FIX: ModelViews can have Resources whose models have unique fields.
ReadModelMixin and UpdateModelMixin store model instance as a property. This allows ModelResource to bind the ModelForm using the model instance making the form validate the input data against the model instance and not a brand new instance. When the latter happened and the model used unique fields, the form validation failed whenever a PUT was maintaining the previuos value of the unique field.
Diffstat (limited to 'djangorestframework/mixins.py')
-rw-r--r--djangorestframework/mixins.py20
1 files changed, 10 insertions, 10 deletions
diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py
index 1b3aa241..3d74ae19 100644
--- a/djangorestframework/mixins.py
+++ b/djangorestframework/mixins.py
@@ -491,17 +491,17 @@ class ReadModelMixin(object):
try:
if args:
# If we have any none kwargs then assume the last represents the primrary key
- instance = model.objects.get(pk=args[-1], **kwargs)
+ self.model_instance = model.objects.get(pk=args[-1], **kwargs)
else:
# Otherwise assume the kwargs uniquely identify the model
filtered_keywords = kwargs.copy()
if BaseRenderer._FORMAT_QUERY_PARAM in filtered_keywords:
del filtered_keywords[BaseRenderer._FORMAT_QUERY_PARAM]
- instance = model.objects.get(**filtered_keywords)
+ self.model_instance = model.objects.get(**filtered_keywords)
except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
- return instance
+ return self.model_instance
class CreateModelMixin(object):
@@ -540,19 +540,19 @@ class UpdateModelMixin(object):
try:
if args:
# If we have any none kwargs then assume the last represents the primrary key
- instance = model.objects.get(pk=args[-1], **kwargs)
+ self.model_instance = model.objects.get(pk=args[-1], **kwargs)
else:
# Otherwise assume the kwargs uniquely identify the model
- instance = model.objects.get(**kwargs)
+ self.model_instance = model.objects.get(**kwargs)
for (key, val) in self.CONTENT.items():
- setattr(instance, key, val)
+ setattr(self.model_instance, key, val)
except model.DoesNotExist:
- instance = model(**self.CONTENT)
- instance.save()
+ self.model_instance = model(**self.CONTENT)
+ self.model_instance.save()
- instance.save()
- return instance
+ self.model_instance.save()
+ return self.model_instance
class DeleteModelMixin(object):