Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MaskedCreditCardField(models.CharField):
- def __init__(self, *args, **kwargs):
- kwargs['max_length'] = 20
- super().__init__(*args, **kwargs)
- def to_python(self, value):
- if not isinstance(value, str):
- raise ValidationError("The card number must be a string")
- if not value.isdigit():
- raise ValidationError("The card number must contain only digits")
- if len(value) != 16:
- raise ValidationError("The card number must be exactly 16 characters long")
- return f"****-****-****-{value[-4:]}"
- class CreditCard(models.Model):
- card_owner = models.CharField(max_length=100)
- card_number - MaskedCreditCardField()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement