forms组件源码剖析

一:forms组件源码剖析

1.forms组件源码切入点:
1.0
copy
form_obj.is_valid()
2.0
copy
def is_valid(self): """ Returns True if the form has no errors. Otherwise, False. If errors are being ignored, returns False. """ return self.is_bound and not self.errors # 如果is_valid要返回True的话 那么self.is_bound要为True self.errors要为Flase
3.0 is_bound深入
copy
self.is_bound = data is not None or files is not None # 只要你传值了肯定为True
4.0 errors深入
copy
@property def errors(self): "Returns an ErrorDict for the data provided for the form" if self._errors is None: # _errors默认就为None self.full_clean() # 肯定会走 return self._errors
5.0
  • full_clean就是form组件的精髓所在,效验字段和局部钩子与全局钩全在该方法内
copy
# forms组件所有的功能基本都出自于该方法 def full_clean(self): # 创建了一个空字典 self._errors = ErrorDict() if not self.is_bound: # 有数据传进来的时候,该判断不会走 return self.cleaned_data = {} # 效验通过的数据 if self.empty_permitted and not self.has_changed(): # 该方法不会执行 return # _代表私有的 外部不能调用 self._clean_fields() # 校验字段 + 局部钩子 self._clean_form() # 全局钩子 self._post_clean() #
posted @   AlexEvans  阅读(47)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
🚀