Added an exception to only auto-complete field when whe're creating new objects, during POST methods.

This commit is contained in:
Lars Wiegman 2012-01-14 14:26:28 +01:00
parent 5fb50c35e5
commit aac479c48b

View File

@ -108,17 +108,17 @@ class FormResource(Resource):
# Otherwise, what's the point of having a default value in the model if # Otherwise, what's the point of having a default value in the model if
# they aren't used during form validation. # they aren't used during form validation.
# A better place for this would be in Django forms or db module. # A better place for this would be in Django forms or db module.
if hasattr(self.model, '_meta'): method = self.view._method if hasattr(self.view, '_method') else None
if 'POST' == method and hasattr(self.model, '_meta'):
data_mutable = data.copy() data_mutable = data.copy()
for field in self.model._meta.fields: for field in self.model._meta.fields:
# Exclude the id and pk # Gather excludes like id and pk from self and Meta
exclude = self.exclude exclude = self.exclude + tuple(data.keys())
try: try:
# Excludes set in ModelForm
exclude += self.form.Meta.exclude exclude += self.form.Meta.exclude
except AttributeError: except AttributeError:
pass pass
if field.has_default() and field.name not in data and field.name not in exclude: if field.name not in exclude and field.has_default():
default = field.get_default() default = field.get_default()
data_mutable.update({field.name:default}) data_mutable.update({field.name:default})
data = data_mutable data = data_mutable