I have the following two model classes in django rest framework
class Customer(models.Model):
GENDER_CHOICES = [
('Male', 'Male'),
('Female', 'Female'),
]
customer_id = models.AutoField(primary_key=True)
firstname = models.CharField(max_length=20)
lastname = models.CharField(max_length=30)
gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
date_of_birth = models.DateField()
nationality = models.CharField(max_length=50, null=False)
id_card_number = models.CharField(max_length=20, unique=True)
passport_number = models.CharField(max_length=20, unique=True)
address = models.CharField(max_length=100)
phone_number = models.CharField(max_length=20)
email = models.EmailField(max_length=254,null=True, blank=True, unique=True)
def __str__(self):
return f"{self.firstname} {self.lastname}"
class Contract(models.Model):
INSURANCE_TYPE_CHOICES = [
('Health', 'Health Insurance'),
('Life', 'Life Insurance'),
('Auto', 'Auto Insurance'),
('Home', 'Home Insurance'),
]
contract_number = models.AutoField(primary_key=True)
start_date = models.DateField()
end_date = models.DateField()
insurance_type = models.CharField(max_length=15, choices=INSURANCE_TYPE_CHOICES)
insurance_holder = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name="contracts")
active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.contract_number} {self.insurance_type}"
and the following two serializers
class ContractSerializer(serializers.ModelSerializer):
insurance_holder = serializers.StringRelatedField(read_only=True)
class Meta:
model = Contract
exclude = ("updated_at",)
class CustomerSerializer(serializers.ModelSerializer):
contracts = ContractSerializer(many=True, read_only=True)
class Meta:
model = Customer
fields = "__all__"
I have also created my url endpoints and my views, i have tested my api through Postman and the django-rest built in
browasable API and everything is fine but the problem i have is the following.
The insurance holder is a foreign key of my customers model so when i do get requests in contracts endpoint i get the Id
instead of the full name so I set the insurance holder in the serializer as StringRelatedField to get the fullname, however this is read only field so i am not able to post requests in contracts with the fullname. How can i make post requests with the name but retain the foreign key constraint of the models.
By the way i am new in Django and Django Rest Framework and novice just in general.
I already read django rest documentation about related fields and the following stack overflow post How can I show the StringRelatedField instead of the Primary Key while still being able to write-to that field using Django Rest Framework? but i still don’t understand how to solve the problem..
Harrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.