title: How to create a created_by field in django rest framework model?
body: <p>I want to create a created_by field in django rest framework model.</p>
  
  <p>It should contain, user_id of currently logged in user and it should be such that I need not send it during POST request in JSON format, it should be taken from request itself, how can I implement it using django rest framework serializer.</p>
  
  <p>models.py file:</p>
  
  <pre><code>class Company(models.Model):
      """Company model for information about company."""
  
      created_by = models.ForeignKey(User, null= True, blank= True, on_delete=models.CASCADE)
      company_name = models.CharField(max_length=100)
  </code></pre>
  
  <p>serializers.py file:</p>
  
  <pre><code>class CompanySerializer(serializers.ModelSerializer):
  """Serializer for companies."""
  
      class Meta:
          model = Company
          fields = ('id', 'created_by', 'company_name')
  </code></pre>
  
  <p>v1.py file:</p>
  
  <pre><code>class CompanyList(APIView):
  """Get and post companies data."""
  
      def get(self, request, format=None):
          """Get companies data.."""
          companies = Company.objects.all()
          serialized_companies = CompanySerializer(companies, many=True)
          return Response(serialized_companies.data)
  
      def post(self, request, format=None):
          """Post companies data."""
          serializer = CompanySerializer(data=request.data)
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data, status=status.HTTP_201_CREATED)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>