ModelForm Actions

1  ModelForm
 2      a.   class Meta:
 3              model,                            #Corresponding to Model 
4              fields=None,                      #Field 5              exclude=None,                     #Exclude field 
6              labels=None,                      #Prompt information 7              help_texts=None, #Help                  prompt information 8 widgets              = None,                     #custom plugin 9              error_messages=None              , #



Custom error information (overall error information from django.core.exceptions import NON_FIELD_ERRORS) 
10              field_classes=None #custom                field classes (you can also customize fields) 
11              localized_fields=( ' birth_date ' ,) #localization , such as: according to different Time zone display data 
12              such as:
 13                  in the database
 14                      2016-12-27 04:10:57
 15                  configuration in the setting
 16                      TIME_ZONE = ' Asia/Shanghai ' 
17                      USE_TZ = True
 18                  then display:
 19                     2016-12-27 12:10:57
 20      b. Validation execution process
 21          is_valid -> full_clean -> hook -> overall error
 22   
23      c. Dictionary field validation
 24          def clean_fieldname(self):
 25              #can throw exception 
26              # from django.core.exceptions import ValidationError 
27              return  " new value " 
28      d. for validation
 29          model_form_obj = XXOOModelForm()
 30          model_form_obj.is_valid()
 31          model_form_obj.errors.as_json()
 32          model_form_obj.clean()
 33         model_form_obj.cleaned_data
 34      e. Used to create
 35          model_form_obj = XXOOModelForm(request.POST)
 36          # ### The page is displayed and submitted ##### 
37 #Default          save many-to-many 38              obj = form.save(commit= True )
 39 #Do nothing, define save_m2m internally (for saving many-to-many) 40              obj = form.save(commit= False)
 41              obj.save() #Save       single table information 42              obj.save_m2m()   #Save association Many-to-many information 43 44     f. For update and initialization
 45          obj = model.tb.objects.get(id=1 )

         


  
 46         model_form_obj = XXOOModelForm(request.POST,instance=obj)
47         ...
48  
49         PS: 单纯初始化
50             model_form_obj = XXOOModelForm(initial={...})
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938405&siteId=291194637