[Django] #35820: Non-editable error for a GenericForeignKey in an ModelForm when updating to version 5.1

12 views
Skip to first unread message

Django

unread,
Oct 7, 2024, 4:14:50 PM10/7/24
to django-...@googlegroups.com
#35820: Non-editable error for a GenericForeignKey in an ModelForm when updating to
version 5.1
-------------------------------+-----------------------------------------
Reporter: Arthur Hanson | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35820>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 7, 2024, 4:16:05 PM10/7/24
to django-...@googlegroups.com
#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:
New description:
--
--
Ticket URL: <https://code.djangoproject.com/ticket/35820#comment:1>

Django

unread,
Oct 8, 2024, 4:08:42 AM10/8/24
to django-...@googlegroups.com
#35820: Non-editable error for a GenericForeignKey in an ModelForm when updating to
version 5.1
-------------------------------+--------------------------------------
Reporter: Arthur Hanson | Owner: (none)
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => invalid
* status: new => closed

Comment:

This works fine for me
{{{#!diff

+class TaggedItemWithContentObjectForm(forms.ModelForm):
+ content_object = forms.CharField(max_length=10)
+
+ class Meta:
+ model = TaggedItem
+ fields = ["tag", "content_type", "object_id"]
+
+ def clean(self):
+ # Ignore content_object.
+ self.cleaned_data.pop("content_object")
+ return self.cleaned_data
+
+
class GenericInlineFormsetTests(TestCase):
def test_output(self):
GenericFormSet = generic_inlineformset_factory(TaggedItem,
extra=1)
@@ -217,6 +230,18 @@ class GenericInlineFormsetTests(TestCase):
with self.assertRaisesMessage(Exception, msg):
generic_inlineformset_factory(BadModel, TaggedItemForm)

+ def test_gfk_form(self):
+ quartz = Mineral.objects.create(name="Quartz", hardness=7)
+ ctype = ContentType.objects.get_for_model(quartz)
+ data = {
+ "tag": "lizard",
+ "content_type": ctype.pk,
+ "object_id": quartz.pk,
+ "content_object": "ignored",
+ }
+ form = TaggedItemWithContentObjectForm(data=data)
+ self.assertEqual(form.is_valid(), True)
+
def test_save_new_uses_form_save(self):
}}}

I get an error if I add `content_object` to fields which you don't need to
do
--
Ticket URL: <https://code.djangoproject.com/ticket/35820#comment:2>
Reply all
Reply to author
Forward
0 new messages