def validate_mobile(self, value): # pylint: disable=no-self-use ''' 校验手机 ''' if is_mobile(value): return value raise ValidationError('invalid')
def save(self, *args, **kwargs): # pylint: disable=arguments-differ,signature-differs for unique_feilds in [ 'username', 'email', 'mobile', 'private_email', ]: value = getattr(self, unique_feilds) if not value: continue existed = False if unique_feilds == 'mobile': if not is_mobile(value): raise ValidationError({unique_feilds: ['invalid']}) if is_i18n_mobile(value) and is_cn_mobile(value): existed = User.valid_objects.filter( models.Q(mobile=value) | models.Q(mobile=value.replace(CN_MOBILE_PREFIX, "")) ).exclude(pk=self.pk).exists() # pylint: disable=no-member elif is_native_mobile(value): existed = User.valid_objects.filter( models.Q(mobile=value) | models.Q(mobile=CN_MOBILE_PREFIX + value)).exclude( pk=self.pk).exists() else: existed = User.valid_objects.filter(mobile=value).exclude( pk=self.pk).exists() else: _kwargs = {unique_feilds: value} existed = User.valid_objects.filter(**_kwargs).exclude( pk=self.pk).exists() if existed: msg = "UNIQUE constraint failed: " + \ f"oneid_meta.User UniqueConstraint(fields=['{unique_feilds}'], condition=Q(is_del='False')" print(msg) raise ValidationError({unique_feilds: ['existed']}) super(User, self).save(*args, **kwargs)