#21428 closed Bug (fixed)
Django 1.6 GenericRelation editable regression
Reported by: | Josh Cartmell | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.6 |
Severity: | Release blocker | Keywords: | GenericRelation, ModelForm, Admin, editable |
Cc: | loic@… | Triage Stage: | Ready for checkin |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Prior to Django 1.6 it was possible to mark a GenericRelation as editable and then add it to the admin fields/fieldsets of a model it was a field of.
The following is a simple Django project/app which demonstrates behavior that works under 1.5 but fails under 1.6. It includes a unittest that similarly works under 1.5 but fails under 1.6.
https://bitbucket.org/joshcartme/16genericissue
I brought this up on the developers mailing list, here is the thread:
https://groups.google.com/forum/#!topic/django-developers/Or7LmK17LIU
The instigation for this ticket is that Mezzanine makes use of GenericRelations in the way mentioned above. We have been trying to figure out how to bring Django 1.6 support to Mezzanine. Here is the Mezzanine ticket:
https://github.com/stephenmcd/mezzanine/issues/745
From that Mezzanine ticket, here is a diff that reverts the changes that cause my simple test case to fail:
https://gist.github.com/loic/7427160
Change History (15)
comment:1 by , 11 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Severity: | Normal → Release blocker |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 11 years ago
Editable GenericRelations weren't designed to work in modelforms, and I guess that is the reason why there is zero tests for this. To see that GenericRelations try to avoid to be editable, see GenericRelation.__init__
which explicitly overrides editable to False. But as Mezzanine relies on this and I don't see any way to overcome this in Mezzanine I think we should fix this in Django.
I have a proposed patch that just adds virtual fields to modelforms if they are editable. I wonder if this is enough to make Mezzanine work in 1.6?
Patch against 1.6 available from https://github.com/akaariai/django/compare/django:stable%2F1.6.x...ticket_21428.
comment:4 by , 11 years ago
Thanks for the patch @akaariai, that does expose the fields in the admin, but Mezzanine is then running into some other problems after saving. I'll see if we can track down whether the remaining problems are with Mezzanine, or Django and post back here.
comment:6 by , 11 years ago
I've just run Mezzanine against akaariai's patch and it's much improved but still a minor issue
Previously in Django 1.5 the GenericRelation's save_form_data method would be called, but this no longer happens in 1.6
comment:7 by , 11 years ago
The made this addition to akaari's patch using the same logic as elsewhere in the patch, which allows save_form_data to be called on the GenericRelation's form field:
-
django/forms/models.py
a b def save_instance(form, instance, fields=None, fail_message='saved', 82 82 # Wrap up the saving of m2m data as a function. 83 83 def save_m2m(): 84 84 cleaned_data = form.cleaned_data 85 for f in opts.many_to_many: 85 for f in (opts.virtual_fields + opts.many_to_many): 86 if not getattr(f, 'editable', False): 87 continue 86 88 if fields and f.name not in fields: 87 89 continue 88 90 if exclude and f.name in exclude:
comment:10 by , 11 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Pull request here: https://github.com/django/django/pull/1914. I'll mark it ready for checkin but leave it open for a day or two if somebody wants to review.
Please open separate ticket for the Admin issue.
EDIT: Removed request for test case - that already exists in the PR for the admin issue.
comment:11 by , 11 years ago
Thanks @akaariai for the help so far, sorry that I dropped of the grid there for a few days.
Is the admin issue you want a separate ticket for the one addressed by this patch of yours:
https://github.com/akaariai/django/compare/django:stable%2F1.6.x...ticket_21428.
follow-up: 17 comment:12 by , 11 years ago
Admin issue is tracked in #21431 and a proposed fix is in https://github.com/akaariai/django/commit/c895dd39ae4e7687b57ff01ecfcc27caf70323ed. Could you verify the proposed fix works for Mezzanine?
comment:13 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:17 by , 11 years ago
Replying to akaariai:
Admin issue is tracked in #21431 and a proposed fix is in https://github.com/akaariai/django/commit/c895dd39ae4e7687b57ff01ecfcc27caf70323ed. Could you verify the proposed fix works for Mezzanine?
I can confirm the merged fix resolves the issue in Mezzanine - thanks so much for your help!
Once #21431 is resolved we'll be able to release the next Mezzanine compatible with Django 1.6
If you are able to write a test for Django's test suite itself instead of as a separate project, that will help us expedite this fix.