I need to get a json response with "customer" fields and the first phone number associated.
# models.py
class Customer(models.Model):
name = models.CharField(max_length=255)
surname = models.CharField(max_length=255,)
...
class CustomerPhone(models.Model):
customer = models.ForeignKey(Customer, related_name = 'phones', on_delete=models.CASCADE)
phone_number = PhoneNumberField()
...
My customer can have more phone numbers but in summary table I would like to see only the first phone (with min id)
When I try with this queryset:
Customer.objects.all().values('surname','phones__phone_number')
I obtain
<QuerySet [{'surname': 'Gates', 'phones__phone_number': '+39123456789'}, {'surname': 'Gates', 'phones__phone_number': '+3998765431'}, {'surname': 'Trump', 'phones__phone_number': '+32123456001'}, {'surname': 'Trump', 'phones__phone_number': '+3298765000'}]>
What I can obtain only one result for customer with only one phone number? what can I get