Detailed explanation of ModelForm component of based on Django
- 2020-06-15 09:27:19
- OfStack
Create a class
from django.forms import ModelForm
from django.forms import widgets as wd
from app01 import models
class The name of the class (ModelForm):
class Meta:
model = models. The name of the table #models In the name of the table
fields="__all__", # field
exclude=None, # Rule out field
widgets=None, # Custom plug-in
error_messages=None, # Custom error messages (overall error messages) from django.core.exceptions import NON_FIELD_ERRORS )
def clean_ The field name # Custom hook functions
Instantiate object
form = Class name (instance=obj,data= request.POST)
instance passes an existing object (displays default values on the page)
data passes the value returned from the page (the value entered by the user for validation)
form. is_valid (#)
form. save # save the data to the database (modify the record in the database if there is an instance parameter and add a new record in the database if there is no instance parameter)