I'm trying to create a Client
Model with an email field that can contains one or multiple email addresses.
I know that there's the EmailField
type that accepts a single email address, but in my case, I need to extend this to accept multiple email addresses:
class Client(models.Model):
company_name = models.CharField(max_length=100, unique=True)
project = models.ForeignKey(Project, blank=True, null=True,
on_delete=models.SET_NULL)
email_list = models.EmailField(max_length=70)
How can I achieve this to extend email_list
capacity for multiple email addresses?
The EmailField
can't be used for list.
You can create another model and use a ForeignKey
class Client(models.Model):
company_name = models.CharField(max_length=100, unique=True)
project = models.ForeignKey(Project, blank=True, null=True, on_delete=models.SET_NULL)
class Email(models.Model):
client = models.ForeignKey(Client on_delete=models.CASCADE)
email_list = models.EmailField(max_length=70)
Or a CharField like that:
class Client(models.Model):
company_name = models.CharField(max_length=100, unique=True)
project = models.ForeignKey(Project, blank=True, null=True, on_delete=models.SET_NULL)
email_list = models.CharField(max_length=1000)
Client.objects.create(company_name="test", project=project_instance, email_list=json.dumps(email_list))
But the second one, I think, is not good, and you will lost the EmailField
validate.