[Answer]-Django ORM Confusion with Query

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 emails 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)
👤Yossi

Leave a comment