|
| 1 | +Forms |
| 2 | +===== |
| 3 | + |
| 4 | +If NO custom forms defined in CRUD class, then by default crudbuilder will generate modelform from Django modelform factory. |
| 5 | + |
| 6 | +Custom Modelform in CRUD class |
| 7 | +------------------------------ |
| 8 | + |
| 9 | +You can define your own custom modelform in yourapp/forms.py and the same will be used for CRUD class. As shown below:: |
| 10 | + |
| 11 | + # yourapp/forms.py |
| 12 | + class PersonEmploymentForm(forms.ModelForm): |
| 13 | + class Meta: |
| 14 | + model = PersonEmployment |
| 15 | + fields = '__all__' |
| 16 | + # exclude = ('person',) |
| 17 | + |
| 18 | + # yourapp/crud.py |
| 19 | + class PersonEmploymentCrud(BaseCrudBuilder): |
| 20 | + model = PersonEmployment |
| 21 | + custom_modelform = PersonEmploymentForm |
| 22 | + |
| 23 | + |
| 24 | +Separate CREATE and UPDATE forms |
| 25 | +-------------------------------- |
| 26 | + |
| 27 | +You can also define separate forms for CreateView and UpdateView.:: |
| 28 | + |
| 29 | + # yourapp/forms.py |
| 30 | + class PersonEmployementCreateForm(forms.ModelForm): |
| 31 | + class Meta: |
| 32 | + model = PersonEmployment |
| 33 | + exclude = ('person', 'medical_allowance') |
| 34 | + |
| 35 | + class PersonEmployementUpdateForm(forms.ModelForm): |
| 36 | + class Meta: |
| 37 | + model = PersonEmployment |
| 38 | + exclude = ('salary', 'year') |
| 39 | + |
| 40 | + # youapp/crud.py |
| 41 | + class PersonEmploymentCrud(BaseCrudBuilder): |
| 42 | + model = PersonEmployment |
| 43 | + createupdate_forms = { |
| 44 | + 'create': PersonEmployementCreateForm, |
| 45 | + 'update': PersonEmployementUpdateForm |
| 46 | + } |
| 47 | + |
| 48 | +You can check `forms`_.py of example project on Github. |
| 49 | + |
| 50 | + |
| 51 | +.. _forms: https://github.com/asifpy/django-crudbuilder/tree/master/crudbuilder/templates |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
0 commit comments