title: How can I map objects in Django?
body: <p>Let's assume that I have in my POST method three fields - <code>field1</code>, <code>field2</code>, <code>field3</code> however in POST method <code>field2</code> and <code>field3</code> should have the same value during adding to the database. So there is no need to add for instance <code>field2=2</code>, <code>field3=2</code> manually. I could add only  <code>field1=1</code>, <code>field2=2</code> and then <code>field3=2</code> will be added to this body automatically and finally whole 3 fields will be added to the database.  I wonder how it should be done in most optimal way? Should it be done in view? I try in the way shown below.</p>
  
  <p>views.py:</p>
  
  <pre><code>class UserObject(GenericAPIView):
  
      def get_serializer_class(self, *args, **kwargs):
          if self.request.method == 'POST':
              return ObjectPostSerializer
          return ObjectSerializer
  
      def post(self, request, user_id):
  
          serializer = ObjectPostSerializer(data=request.data)
          if serializer.is_valid():
              serializer.save(user_id=user_id)
              return Response(serializer.data, status=status.HTTP_201_CREATED)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>serializers.py:</p>
  
  <pre><code>class ObjectPostSerializer(serializers.ModelSerializer):
  
      def __init__(self, *args, **kwargs):
          super(ObjectPostSerializer, self).__init__(*args, **kwargs)
  
          for key in self.fields:
              self.fields['field3'].required = False
  
      class Meta:
  
          model=Object
          field3 = model.field2
  
          fields = ('__all__')
  </code></pre>