From d43156e1e984d7e0848b7d4db218877a29d617b4 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 20 Feb 2016 14:40:07 +0100 Subject: Fixed #26238 -- Raised explicit error for non-editable field in ModelForm Thanks Luke Crouch for the report and Simon Charette for the review. --- django/forms/models.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'django/forms') diff --git a/django/forms/models.py b/django/forms/models.py index 8e8c8cfb55..be9e63d144 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -148,6 +148,12 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None, if isinstance(f, ModelField)] for f in sorted(chain(opts.concrete_fields, sortable_virtual_fields, opts.many_to_many)): if not getattr(f, 'editable', False): + if (fields is not None and f.name in fields and + (exclude is None or f.name not in exclude)): + raise FieldError( + "'%s' cannot be specified for %s model form as it is a non-editable field" % ( + f.name, model.__name__) + ) continue if fields is not None and f.name not in fields: continue -- cgit v1.3