1👍
The schema you describe is sub-optimal. Using this schema will result in consistency issues when only some of the user’s data will be updated/deleted.
If you want the Customer
to have multiple email
s if would be wiser to create a separate Email
model or use array field.
class Customer(models.Model):
name = models.CharField(max_length=50)
class CustomerEmail(models.Model):
customer = models.ForeignKey(Customer, related_name='emails')
email = models.EmailField()
Now it becomes easy make the query you want.
customers = Customer.objects.select_related('emails')
And get their emails:
for email in customers.emails:
print(email.email)
Source:stackexchange.com