From 0e079e4331a8be4dbd18d5e5776116330b0a5e61 Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Tue, 12 Nov 2013 21:35:52 +0200 Subject: Fixed #21428 -- editable GenericRelation regression The GenericRelation refactoring removed GenericRelations from model._meta.many_to_many. This had the side effect of disallowing editable GenericRelations in ModelForms. Editable GenericRelations aren't officially supported, but if we don't fix this we don't offer any upgrade path for those who used the ability to set editable=True in GenericRelation subclass. Thanks to Trac alias joshcartme for the report and stephencmd and Loic for working on this issue. --- django/forms/models.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'django/forms') diff --git a/django/forms/models.py b/django/forms/models.py index 6d9a567926..610a2a176a 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -83,7 +83,12 @@ def save_instance(form, instance, fields=None, fail_message='saved', # Wrap up the saving of m2m data as a function. def save_m2m(): cleaned_data = form.cleaned_data - for f in opts.many_to_many: + # Note that for historical reasons we want to include also + # virtual_fields here. (GenericRelation was previously a fake + # m2m field). + for f in opts.many_to_many + opts.virtual_fields: + if not hasattr(f, 'save_form_data'): + continue if fields and f.name not in fields: continue if exclude and f.name in exclude: @@ -119,8 +124,8 @@ def model_to_dict(instance, fields=None, exclude=None): from django.db.models.fields.related import ManyToManyField opts = instance._meta data = {} - for f in opts.concrete_fields + opts.many_to_many: - if not f.editable: + for f in opts.concrete_fields + opts.virtual_fields + opts.many_to_many: + if not getattr(f, 'editable', False): continue if fields and not f.name in fields: continue @@ -174,8 +179,8 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None, field_list = [] ignored = [] opts = model._meta - for f in sorted(opts.concrete_fields + opts.many_to_many): - if not f.editable: + for f in sorted(opts.concrete_fields + opts.virtual_fields + opts.many_to_many): + if not getattr(f, 'editable', False): continue if fields is not None and not f.name in fields: continue -- cgit v1.3