﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35820	Non-editable error for a GenericForeignKey in an ModelForm when updating to version 5.1	Arthur Hanson		"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. "	Uncategorized	new	Uncategorized	5.0	Normal				Unreviewed	0	0	0	0	0	0
