#36151: Non-editable FieldError raised when using a GenericForeignKey on a form.
--------------------------------------+------------------------------------
     Reporter:  James Pic             |                    Owner:  (none)
         Type:  Bug                   |                   Status:  new
    Component:  contrib.contenttypes  |                  Version:  5.1
     Severity:  Release blocker       |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  1                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

 * cc: Adam Johnson, Mariusz Felisiak (added)
 * severity:  Normal => Release blocker
 * stage:  Unreviewed => Accepted
 * summary:  editable GenericForeignKey =>
     Non-editable FieldError raised when using a GenericForeignKey on a
     form.
 * type:  Cleanup/optimization => Bug

Comment:

 This looks fiddly, but the following test fails on main, but was passing
 on 5.0 and is a regression in 6002df06713cb0a7050432263527a25754190c27

 On main, the error is `django.core.exceptions.FieldError: 'question'
 cannot be specified for Answer model form as it is a non-editable field`.
 {{{#!diff
 --- a/tests/contenttypes_tests/test_fields.py
 +++ b/tests/contenttypes_tests/test_fields.py
 @@ -1,5 +1,6 @@
  import json

 +from django import forms
  from django.contrib.contenttypes.fields import GenericForeignKey
  from django.contrib.contenttypes.prefetch import GenericPrefetch
  from django.db import models
 @@ -65,6 +66,17 @@ class GenericForeignKeyTests(TestCase):
          answer.refresh_from_db()
          self.assertIsNot(answer.question, old_question_obj)

 +    def test_form_field(self):
 +        class AnswerForm(forms.ModelForm):
 +            question = forms.CharField()
 +
 +            class Meta:
 +                model = Answer
 +                fields = ["question"]
 +
 +        form = AnswerForm()
 +        self.assertIn("question", form.fields)
 +

  class GenericRelationTests(TestCase):
      def test_value_to_string(self):
 }}}

 However, note that `question` was excluded by default on the form.

 Looks like this was also raised here: https://forum.djangoproject.com/t
 /non-editable-error-for-a-genericforeignkey-in-an-modelform-when-updating-
 to-version-5-1/35033
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36151#comment:5>
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 visit 
https://groups.google.com/d/msgid/django-updates/01070194ad4d8d73-3fac322e-161e-4185-9341-f4e826db6b60-000000%40eu-central-1.amazonses.com.

Reply via email to