Autocompleet fields if they have a default value set in the model. Otherwise, what's the point of having a default value in the model if they aren't used during form validation. A better place for this would be in Django forms or db module.

This commit is contained in:
Lars Wiegman 2012-01-13 18:54:55 +01:00
parent c71b6fb090
commit 5fb50c35e5

View File

@ -104,6 +104,25 @@ class FormResource(Resource):
to be populated when an empty dict is supplied in `data`
"""
# Autocompleet fields if they have a default value set in the model.
# Otherwise, what's the point of having a default value in the model if
# they aren't used during form validation.
# A better place for this would be in Django forms or db module.
if hasattr(self.model, '_meta'):
data_mutable = data.copy()
for field in self.model._meta.fields:
# Exclude the id and pk
exclude = self.exclude
try:
# Excludes set in ModelForm
exclude += self.form.Meta.exclude
except AttributeError:
pass
if field.has_default() and field.name not in data and field.name not in exclude:
default = field.get_default()
data_mutable.update({field.name:default})
data = data_mutable
# We'd like nice error messages even if no content is supplied.
# Typically if an empty dict is given to a form Django will
# return .is_valid() == False, but .errors == {}