本文整理汇总了Python中django.db.models.Model.add_to_class方法的典型用法代码示例。如果您正苦于以下问题:Python Model.add_to_class方法的具体用法?Python Model.add_to_class怎么用?Python Model.add_to_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.Model
的用法示例。
在下文中一共展示了Model.add_to_class方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: from django.db.models import Model [as 别名]
# 或者: from django.db.models.Model import add_to_class [as 别名]
def __new__(cls, name, bases, attrs):
module = attrs.pop("__module__")
parents = [b for b in bases if isinstance(b, ModelMetaClass)]
if parents:
for obj_name, obj in attrs.items():
Model.add_to_class(obj_name, obj)
return super(ModelMetaClass, cls).__new__(cls, name, bases, attrs)
示例2:
# 需要导入模块: from django.db.models import Model [as 别名]
# 或者: from django.db.models.Model import add_to_class [as 别名]
More info:
* "Why doesn't django's model.save() call full clean?"
http://stackoverflow.com/questions/4441539/
* "Model docs imply that ModelForm will call Model.full_clean(),
but it won't."
https://code.djangoproject.com/ticket/13100
"""
try:
self.full_clean()
self.save(*args, **kwargs)
return True
except ValidationError:
e = sys.exc_info()[1]
self._errors = {}
for k, v in e.message_dict.items():
self._errors[k] = v
if throw_exception:
raise
return False
Model.add_to_class('_errors', {})
Model.add_to_class(SMART_SAVE_METHOD, save_if_valid)