#18168: formfield_for_choice_field() doesn't validate choices updated in method
-------------------------------------+-------------------------------------
     Reporter:  Robert               |                    Owner:  nobody
  <weglarek.robert+djangotrac@…>     |                   Status:  new
         Type:  Bug                  |                  Version:  1.3
    Component:  contrib.admin        |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  admin, inlines       |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

 * stage:  Unreviewed => Accepted


Comment:

 There is no traceback -- the validation error is displayed in the admin.

 The reporter's example uses inlines. I could reproduce the issue with the
 following code. Screenshot attached.

 {{{
 # models.py

 from django.db import models1

 class Variable(models.Model):
     NAME_CHOICES = (
         ('foo', 'foo'),
         ('bar', 'bar'),
         ('baz', 'baz'),
     )
     name  = models.CharField(max_length=3, choices=NAME_CHOICES)

 # admin.py

 from django.contrib import admin
 from .models import Variable

 class VariableAdmin(admin.ModelAdmin):

     def formfield_for_choice_field(self, db_field, request, **kwargs):
         if db_field.name == "name":
             kwargs['choices'] = (
                 ('quux', 'quux'),
                 ('quuux', 'quuux'),
             )
         return super(VariableAdmin,
 self).formfield_for_choice_field(db_field, request, **kwargs)

 admin.site.register(Variable, VariableAdmin)
 }}}

 ----

 I don't think it makes sense to allow in the form values that aren't
 allowed by the model. If I understand correctly this method can only be
 used to restrict changes, not to extend them. If that's true, it should be
 mentioned in the documentation of formfield_for_choice_field.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18168#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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to