Garfonzo
Garfonzo

Reputation: 3966

Django ModelForm fields - must have more than one?

I have a model which has upwards of 25 fields. I want to create a form to edit only ONE of those fields. Thus I did this:

class My_Model_Form(ModelForm):
    class Meta:
        model = myModel
        fields = ('myField')

This gave me errors about Unknown field(s) (i, b, l, o, T) specified for My_Model_Form. So, I changed the above to:

class My_Model_Form(ModelForm):
    class Meta:
        model = myModel
        field = ('myField')

(I removed the s from fields). This worked (as in the runserver command runs) however, that form now includes all fields from the model myModel. The only way I can get my desired one-field form is to use the exclude keyword, and list all fields except the one I want. This seems ridiculous.

Am I am going at this all wrong. Is there a better/correct way to do this?

Thanks!

Upvotes: 2

Views: 873

Answers (1)

Tomasz Wysocki
Tomasz Wysocki

Reputation: 11578

You must have a comma in single-item tuples, otherwise when you iterate over fields it will iterate over each character in the string:

fields = ('myField',)

Upvotes: 10

Related Questions