Inline formset in Django - removing certain fields

前端 未结 3 1018
暖寄归人
暖寄归人 2021-01-31 23:10

I need to create an inline formset which

a) excludes some fields from MyModel being displayed altogether

b) displays some some fields MyMode

3条回答
  •  灰色年华
    2021-01-31 23:23

    Is this a formset for use in the admin? If so, just set "exclude = ['field1', 'field2']" on your InlineModelAdmin to exclude fields. To show some fields values uneditable, you'll have to create a simple custom widget whose render() method just returns the value, and then override the formfield_for_dbfield() method to assign your widget to the proper fields.

    If this is not for the admin, but a formset for use elsewhere, then you should make the above customizations (exclude attribute in the Meta inner class, widget override in __init__ method) in a ModelForm subclass which you pass to the formset constructor. (If you're using Django 1.2 or later, you can just use readonly_fields instead).

    I can update with code examples if you clarify which situation you're in (admin or not).

提交回复
热议问题