| 241 | | 1. Set ``editable=False`` on the model field. As a result, *any* form |
|---|
| 242 | | created from the model via ``ModelForm`` will not include that |
|---|
| 243 | | field. |
|---|
| 244 | | |
|---|
| 245 | | 2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta`` class. |
|---|
| 246 | | This attribute, if given, should be a list of field names to include in |
|---|
| 247 | | the form. |
|---|
| 248 | | |
|---|
| 249 | | 3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta`` class. |
|---|
| 250 | | This attribute, if given, should be a list of field names to exclude |
|---|
| 251 | | the form. |
|---|
| 252 | | |
|---|
| 253 | | For example, if you want a form for the ``Author`` model (defined above) |
|---|
| 254 | | that includes only the ``name`` and ``title`` fields, you would specify |
|---|
| 255 | | ``fields`` or ``exclude`` like this:: |
|---|
| 256 | | |
|---|
| 257 | | class PartialAuthorForm(ModelForm): |
|---|
| 258 | | class Meta: |
|---|
| 259 | | model = Author |
|---|
| 260 | | fields = ('name', 'title') |
|---|
| 261 | | |
|---|
| 262 | | class PartialAuthorForm(ModelForm): |
|---|
| 263 | | class Meta: |
|---|
| 264 | | model = Author |
|---|
| 265 | | exclude = ('birth_date',) |
|---|
| 266 | | |
|---|
| 267 | | Since the Author model has only 3 fields, 'name', 'title', and |
|---|
| 268 | | 'birth_date', the forms above will contain exactly the same fields. |
|---|
| | 241 | 1. Set ``editable=False`` on the model field. As a result, *any* form |
|---|
| | 242 | created from the model via ``ModelForm`` will not include that |
|---|
| | 243 | field. |
|---|
| | 244 | |
|---|
| | 245 | 2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta`` |
|---|
| | 246 | class. This attribute, if given, should be a list of field names |
|---|
| | 247 | to include in the form. |
|---|
| | 248 | |
|---|
| | 249 | 3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta`` |
|---|
| | 250 | class. This attribute, if given, should be a list of field names |
|---|
| | 251 | to exclude the form. |
|---|
| | 252 | |
|---|
| | 253 | For example, if you want a form for the ``Author`` model (defined |
|---|
| | 254 | above) that includes only the ``name`` and ``title`` fields, you would |
|---|
| | 255 | specify ``fields`` or ``exclude`` like this:: |
|---|
| | 256 | |
|---|
| | 257 | class PartialAuthorForm(ModelForm): |
|---|
| | 258 | class Meta: |
|---|
| | 259 | model = Author |
|---|
| | 260 | fields = ('name', 'title') |
|---|
| | 261 | |
|---|
| | 262 | class PartialAuthorForm(ModelForm): |
|---|
| | 263 | class Meta: |
|---|
| | 264 | model = Author |
|---|
| | 265 | exclude = ('birth_date',) |
|---|
| | 266 | |
|---|
| | 267 | Since the Author model has only 3 fields, 'name', 'title', and |
|---|
| | 268 | 'birth_date', the forms above will contain exactly the same fields. |
|---|