How to migrate data for email field
So i have a char field which i was using to store emails. Now there are some rule/validation and processes for email fields like email validation and email normalization etc. I have to add the validation and email normalization on the field while keeping in CharField.
what is the valid and safe ways to do this?
I am using Django and Django REST framework.
What is "DRF"? And what do you mean by validation and normalization?
DRF: django-rest-framework,
validations: like it should have '@' and '.' within.
normalization: making email case in-sensitive.
main issue is already present data.
there can be 2 email which is just different in casing and if i now normalize it, they will be same and collide.
I need a safer way to handle this situation.
Please include your models, and any Serializers you've written.
Model:
class ShareHolder(IsActive):
"""
Model for storing shareholder.
"""
name = models.CharField()
email = models.EmailField(
unique=True,
error_messages={
'unique': EMAIL_EXISTS
}
)
Assume this as user.
and this will be majorly related to other models.
when making any mutation need to be careful about the relation.
There's already an email validator inside the django. you can use it directly.
from django.core.validators import EmailValidator
from django.db import models
class ShareHolder(IsActive):
name = models.CharField(max_length=255)
email = models.CharField(
max_length=254,
unique=True,
validators=[EmailValidator(message="Enter a valid email address.")],
error_messages={"unique": EMAIL_EXISTS},
)