开发者

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
        开发者_StackOverflowfields = ('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!


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',)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜