title: How do you create object with modelviewset and POST request?
body: <p>Im trying to add a comment to my db, but getting error <code>'OrderedDict' object has no attribute 'pk'</code></p>
  <p>The part of <code>react.js</code> code handling the <code>POST</code> request:</p>
  <pre><code>addComment() {
      let url = this.props.post_url
  
      axios.post('/api/comments/', {
            post: url,
            user: &quot;http://127.0.0.1:8000/api/users/1/?format=json&quot;,
            text: document.getElementsByName(url)[0].value,
            csrfmiddlewaretoken: document.getElementsByName(&quot;csrfmiddlewaretoken&quot;)[0].value},
        )
      .then(function (response) {
         console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
  
  }
  </code></pre>
  <p>My serializers.py:</p>
  <pre><code>from django.contrib.auth.models import User
  from rest_framework import serializers
  
  from .models import Post, Comment
  
          
  class UserSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = User
          fields = ('id', 'username', 'url')
  
  
  class CommentSerializer(serializers.HyperlinkedModelSerializer):
      #user = UserSerializer(many=False, required=False)
      class Meta:
          model = Comment
          fields = ('id', &quot;post&quot;, &quot;user&quot;, 'text')
          read_only_fields = ('id', &quot;user&quot;)
  
      def create(self):
          user = None   
          request = self.context.get(&quot;request&quot;)
          if request and hasattr(request, &quot;user&quot;):
              user = request.user
  
  
  class PostSerializer(serializers.HyperlinkedModelSerializer):
      #user = UserSerializer(required=False)
      comments = CommentSerializer(many=True, required=False, read_only=True)
      class Meta:
          model = Post
          fields = ('id', 'title', &quot;user&quot;, &quot;url&quot;, &quot;comments&quot;, 'text')
          read_only_fields = ('id', &quot;url&quot;, &quot;comments&quot;)
  
      def save(self):
          user = None
          request = self.context.get(&quot;request&quot;)
          if request and hasattr(request, &quot;user&quot;):
              user = request.user
  </code></pre>
  <p>My views.py</p>
  <pre><code>from django.contrib.auth.models import User
  from api.serializers import UserSerializer
  from rest_framework import viewsets
  
  from .models import Comment, Post
  from .serializers import CommentSerializer, PostSerializer
  
  
  class UserViewSet(viewsets.ModelViewSet):
      queryset = User.objects.all()
      serializer_class = UserSerializer
  
          
  class CommentViewSet(viewsets.ModelViewSet):
      queryset = Comment.objects.all()
      serializer_class = CommentSerializer
  
         
  class PostViewSet(viewsets.ModelViewSet):
      queryset = Post.objects.all()
      serializer_class = PostSerializer
  </code></pre>
  <p>When sending the Post-request it goes througth normaly. If I remove one of the fields it returnes a <code>400</code>. Now Im getting this <code>500 [Internal server] error</code>.</p>
  <p>AttributeError: 'OrderedDict' object has no attribute 'pk'</p>
  <p>The error seems to be comming from:</p>
  <blockquote>
  <p>/home/halvor1606/.virtualenvs/django-react/local/lib/python2.7/site-packages/rest_framework/relations.py in get_url</p>
  <pre><code>   # Unsaved objects will not yet have a valid URL.
  
     if hasattr(obj, 'pk') and obj.pk in (None, ''):
  
         return None
  </code></pre>
  <p>Here-&gt; lookup_value = getattr(obj, self.lookup_field) ...</p>
  <pre><code>   kwargs = {self.lookup_url_kwarg: lookup_value}
  
     return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
  
     def get_name(self, obj):
  
         return six.text_type(obj)
  </code></pre>
  </blockquote>
  <p>▶ Local vars are as follows:-</p>
  <pre><code>Variable    Value
  
  request      &lt;rest_framework.request.Request object at 0x7f4e59e75b90&gt;
  
  view_name    'post-detail'
  
  obj           OrderedDict([(u'title', u'adskjfj|'), (u'user', &lt;User:      halvor1606&gt;), (u'text', u'kjkldsjf')])
  
  self          HyperlinkedIdentityField(read_only=True, view_name='post-detail')
  
  format        None
  </code></pre>
  <p>Read the other Questions with the same error. Didn't find one that solved my problem.</p>
  <p>Thank you!</p>
  <p><strong>Edit:</strong></p>
  <p>Solved it by adding this to my post serializer:</p>
  <pre><code>def create(self, validated_data):
      tmp_post = validated_data
      user = None   
  
      request = self.context.get(&quot;request&quot;)
      if request and hasattr(request, &quot;user&quot;):
          user = request.user
  
      post = Post.objects.create(
          user=user,
          title=tmp_post['title'],
          text=tmp_post['text'],
      )
  
      return post
  </code></pre>