How make a StringRelatedField that is readonly and declared as ForeignKey available for post requests?

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..

New contributor

Harrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật