#28748: Named groups in choices are not properly validated
-------------------------------------+-------------------------------------
     Reporter:  Scott Stevens        |                    Owner:  François
                                     |  Freitag
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  2.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  fixed
     Keywords:  choices              |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Matthias Kestenholz):

 This patch regresses a common use case of adding lazily translated
 choices. Just apply the following patch:

 {{{
 (.env) ~/Projects/django/tests$ git diff
 diff --git a/tests/i18n/models.py b/tests/i18n/models.py
 index 15b4de57b6..fc801c71cf 100644
 --- a/tests/i18n/models.py
 +++ b/tests/i18n/models.py
 @@ -16,3 +16,10 @@ class Company(models.Model):

      class Meta:
          verbose_name = _('Company')
 +
 +
 +class TranslatedChoicesModel(models.Model):
 +    choice = models.CharField(max_length=10, choices=[
 +        ('a', _('a')),
 +        ('b', _('b')),
 +    ])
 }}}

 Then, run the testsuite:

 {{{
 (.env) ~/Projects/django/tests$ ./runtests.py i18n
 Testing against Django installed in
 '/home/matthias/Projects/django/django' with up to 4 processes
 Creating test database for alias 'default'...
 Cloning test database for alias 'default'...
 Cloning test database for alias 'default'...
 Cloning test database for alias 'default'...
 Cloning test database for alias 'default'...
 Creating test database for alias 'other'...
 Cloning test database for alias 'other'...
 Cloning test database for alias 'other'...
 Cloning test database for alias 'other'...
 Cloning test database for alias 'other'...
 Traceback (most recent call last):
   File "./runtests.py", line 473, in <module>
     options.exclude_tags,
   File "./runtests.py", line 278, in django_tests
     extra_tests=extra_tests,
   File "/home/matthias/Projects/django/django/test/runner.py", line 599,
 in run_tests
     self.run_checks()
   File "/home/matthias/Projects/django/django/test/runner.py", line 561,
 in run_checks
     call_command('check', verbosity=self.verbosity)
   File
 "/home/matthias/Projects/django/django/core/management/__init__.py", line
 141, in call_command
     return command.execute(*args, **defaults)
   File "/home/matthias/Projects/django/django/core/management/base.py",
 line 335, in execute
     output = self.handle(*args, **options)
   File
 "/home/matthias/Projects/django/django/core/management/commands/check.py",
 line 65, in handle
     fail_level=getattr(checks, options['fail_level']),
   File "/home/matthias/Projects/django/django/core/management/base.py",
 line 410, in check
     raise SystemCheckError(msg)
 django.core.management.base.SystemCheckError: SystemCheckError: System
 check identified some issues:

 ERRORS:
 i18n.TranslatedChoicesModel.choice: (fields.E005) 'choices' must be an
 iterable containing (actual value, human readable name) tuples.

 System check identified 1 issue (0 silenced).
 }}}

 I noticed this because I'm running the feincms3 testsuite with
 Django@master as well (https://travis-
 ci.org/matthiask/feincms3/jobs/326453581)

 A possible fix might include checking for `Promise` instances as well in
 `is_value`:

 {{{
     def _check_choices(self):
         from django.utils.functional import Promise
         if not self.choices:
             return []

         def is_value(value):
             # Run this:
             return isinstance(value, str) or isinstance(value, Promise) or
 not is_iterable(value)
             # instead of this:
             # return isinstance(value, str) or not is_iterable(value)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28748#comment:9>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.e43ae75ce54fe905032e22880bd12fbb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to