Opened 6 weeks ago
Last modified 6 weeks ago
#35820 closed Uncategorized
Non-editable error for a GenericForeignKey in an ModelForm when updating to version 5.1 — at Initial Version
Reported by: | Arthur Hanson | Owned by: | |
---|---|---|---|
Component: | Uncategorized | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
As per discussion at https://forum.djangoproject.com/t/non-editable-error-for-a-genericforeignkey-in-an-modelform-when-updating-to-version-5-1/35033
If you have a GFK defined in a model, then create a Form that has a field of the same name you will get a FieldError:
django.core.exceptions.FieldError: 'my_generic_foreign_model' cannot be specified for my_model_form model form as it is a non-editable field
This worked in Django 5.0, but fails when updating to Django 5.1.
It appears when you have a model form with a field that is the same name as in the model (in this case a GFK) it takes the read-only from the model field instead of the form field. For example we have:
class EventRule(): ... action_object_type = models.ForeignKey( to='contenttypes.ContentType', related_name='eventrule_actions', on_delete=models.CASCADE ) action_object_id = models.PositiveBigIntegerField( blank=True, null=True ) action_object = GenericForeignKey( ct_field='action_object_type', fk_field='action_object_id' )
Then in the form:
class EventRuleForm(): ... action_object = forms.CharField( label=_('Action object'), required=True, help_text=_('Webhook name or script as dotted path module.Class') )
In this case we handle action_object in the clean method and use it to set instance.action_object. But now in Django 5.1 it throws the django.core.exceptions.FieldError shown above.
Not sure if this is an undocumented change on purpose or a bug. Will look at getting a smaller repro scenario together.