#35820: Non-editable error for a GenericForeignKey in an ModelForm when
updating to
version 5.1
-------------------------------+--------------------------------------
Reporter: Arthur Hanson | Owner: (none)
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Arthur Hanson:
Old 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.
New 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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/35820#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070192689fded5-7b3df8be-3a3c-439b-b3cc-bb1c4a1c7cae-000000%40eu-central-1.amazonses.com.