1

I have a model with a Charfield in which no choices are set. Now in the admin panel, in the inline admin in which I display this model, I want to present a dropdown with possibilities to fill in (where the choices need to be fetched dynamically and the display is different from the value that I want to fill in the field). How would I make this happen without changing the model?

This is what I tried (not dynamically yet):

class MyModelInline(TabularInline):
    model = Mapping
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'status':
            kwargs['choices'] = (
                ('accepted', 'Accepted'),
                ('denied', 'Denied'),
            )
            return super(MyModelInline, self).formfield_for_choice_field(db_field, **kwargs)
        return super(MyModelInline, self).formfield_for_dbfield(db_field, **kwargs)

However, when I open my model, I get an error __init__() got an unexpected keyword argument 'choices'. I looked at the stack trace and found that this is the __init__ it is talking about:

<path>/django/forms/fields.py in __init__
       214         super(CharField, self).__init__(*args, **kwargs)

What should I change to make this work? I am using Python 3.5 and Django 1.8.14.

physicalattraction
  • 6,485
  • 10
  • 63
  • 122

0 Answers0