title: how to auto populate Django Rest Framework Model fields
body: <p>Trying to auto populate model attributes using Django rest framework </p>
  
  <p><strong>Intro:</strong> I have 2 models <code>Patient</code> and <code>Embryo</code> There is only 1 user who is the superuser. All patients belong to the superuser. One patient can have many embryos but an embryo can have only one patient. </p>
  
  <p><strong>What I want to achieve</strong> The Embryo model has a field called <code>karyotes</code> which is a <code>Charfield</code>. when the user inputs the <code>karyotes</code> the <code>sex</code> of the embryo is auto-populated. The same with down-syndrome (see models below)   </p>
  
  <p><strong>The Error:</strong> The way I have made my models The <code>sex</code> and <code>down_syndrome</code> always show null</p>
  
  <p><a href="https://i.stack.imgur.com/Ylk3j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ylk3j.png" alt="enter image description here"></a></p>
  
  <pre><code>Example (This is not code, just explanation )
      if karyotype == "46,XX" #The embryo is a Female
      if karyotype == "46,XY" #The embryo is a Male 
      if karyotype == "47,XY,+21" #The embryo is a Male and has Down Syndrome           
      if karyotype == "47,XX,+21" #The embryo is a Female and has Down Syndrome            
      else sex == null and down_syndrome == null
  </code></pre>
  
  <p><strong>Below are my models</strong></p>
  
  <pre><code>class PatientsApiView(viewsets.ModelViewSet):
      """ Handles Creating, reading and updating Patients """
  
  class Embryo(models.Model):
      """ A ForeignKey model to the patient """
      patient = models.ForeignKey(Patient, related_name="embryos", on_delete=models.CASCADE)
      code_name = models.CharField(max_length=100)
      karyotype = models.CharField(max_length=100)
      down_syndrome = models.BooleanField(blank=True, null=True)
      created_at = models.DateTimeField(auto_now_add=True)
      updated_at = models.DateTimeField(auto_now=True)
      GENDER_CHOICES = (
          ("M", "Male"),
          ("F", "Female"),    
      )
      sex = models.CharField(blank=True, null=True, max_length=1, choices=GENDER_CHOICES)
  
      def __str__(self):
          return self.code_name
  
  
  """ Below is my attempt to get the desired result """
      def get_sex(self):
          if self.karyotype == "46,XX":
              return self.sex == "F"
          elif self.karyotype == "46,XY":
              return self.sex == "M"
  
      def has_down_syndrome(self):
          if self.karyotype == "47,XY,+21":
              return self.down_syndrome == True and self.sex == "M"
          elif self.karyotype == "47,XX,+21":
              return self.down_syndrome == True and self.sex == "F"
          else:
              return self.down_syndrome == False and self.sex == null
  </code></pre>
  
  <p><strong>Below are my views.py</strong></p>
  
  <pre><code>class EmbroApiView(viewsets.ModelViewSet):
      """Handles Creating, reading and updating Patients"""
  
      serializer_class = serializers.EmbryoSerializer
      queryset = Embryo.objects.all()
      authentication_classes = (TokenAuthentication,)
      filter_backends = (filters.SearchFilter,)
      permission_classes = (IsAuthenticated,)
      search_fields = ("code_name", "karyotype", "sex", "down_syndrome",)
  
      def perform_create(self, serializer):
          serializer.save(pk=self.kwargs.get("pk"))
  </code></pre>
  
  <p><strong>below are my serializers.py</strong> </p>
  
  <pre><code>class EmbryoSerializer(serializers.ModelSerializer):
      class Meta:
          model = Embryo
          fields = ("id", "patient", "code_name", "karyotype", "sex", "down_syndrome", "created_at", "updated_at")
          extra_kwargs = {"sex": {"read_only": True},
                          "down_syndrome": {"read_only": True}
                          }
  </code></pre>
  
  <p>As per @Ruddra 1st advise</p>
  
  <p><a href="https://i.stack.imgur.com/C6DZU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C6DZU.png" alt="enter image description here"></a></p>