from django import forms
from .models import YourModel
class YourModelForm(forms.ModelForm):
class Meta:
model = YourModel
fields = '__all__'
- Import the necessary forms module from Django.
- Import the
YourModel
model from your Django application. - Define a form class
YourModelForm
that inherits from forms.ModelForm
. - Inside the
YourModelForm
class, use the Meta
inner class to specify metadata for the form. - Set the
model
attribute in the Meta
class to YourModel
, linking this form to the YourModel
model. - Define the
fields
attribute in the Meta
class and set it to '__all__'
, indicating that the form should include fields for all the fields in the YourModel
model.