#34723: `TypeError` when loading a Django app with incorrect type of `choices`
-------------------------------------+-------------------------------------
               Reporter:  Natalia    |          Owner:  nobody
  Bidart                             |
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:  Database   |        Version:  dev
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 While reviewing the PR for #31262, I was performing some local changes to
 ensure that the error messages shown by the check framework were accurate
 and correct, and I changed a `choices` definition to be just an int. This
 change made the reloader fail with an exception:

 {{{
   File "/home/nessita/fellowship/projectfromrepo/ticket_31262/models.py",
 line 31, in <module>
     class OtherTestModelForm(forms.ModelForm):
   File "/home/nessita/fellowship/django/django/forms/models.py", line 308,
 in __new__
     fields = fields_for_model(
              ^^^^^^^^^^^^^^^^^
   File "/home/nessita/fellowship/django/django/forms/models.py", line 233,
 in fields_for_model
     formfield = f.formfield(**kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^
   File
 "/home/nessita/fellowship/django/django/db/models/fields/__init__.py",
 line 2384, in formfield
     return super().formfield(
            ^^^^^^^^^^^^^^^^^^
   File
 "/home/nessita/fellowship/django/django/db/models/fields/__init__.py",
 line 2136, in formfield
     return super().formfield(
            ^^^^^^^^^^^^^^^^^^
   File
 "/home/nessita/fellowship/django/django/db/models/fields/__init__.py",
 line 1112, in formfield
     defaults["choices"] = self.get_choices(include_blank=include_blank)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File
 "/home/nessita/fellowship/django/django/db/models/fields/__init__.py",
 line 1048, in get_choices
     choices = list(self.choices)
               ^^^^^^^^^^^^^^^^^^
 TypeError: 'int' object is not iterable
 }}}

 I believe that the code in `Field.get_choices` could be a little more
 robust, so exceptions are not raised on incorrect choices type. Ideally
 we'd let the check framework run and report these issues is a friendlier
 manner.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34723>
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/010701896ee1a047-5aea3114-ae65-46ed-a63f-a24a96df6fd7-000000%40eu-central-1.amazonses.com.

Reply via email to