title: Django Rest Framework unittest ValueError: Cannot assign "u'johnz'": "Message.author" must be a "User" instance
body: <p><code>tests.py</code> </p>
  
  <pre><code>from django.contrib.auth.models import User
  from django.core.urlresolvers import reverse
  from rest_framework import status
  from rest_framework.test import APITestCase, APIClient, RequestsClient, APIRequestFactory
  from rest_framework.authtoken.models import Token
  from views import MessageListView
  
  
  class CreateMessageTest(APITestCase):
      def setUp(self):
          self.client = APIRequestFactory()
          self.user = User.objects.create_user(username='johnz', first_name='John', last_name='Doe', email='john@doe.com', password='johnny', id=3)
          self.user.save()
          self.token = Token.objects.create(user=self.user)
          self.data = {"body": "I am sad", "author": self.user}
  
      def test_loggedin_user_can_create_message(self):
          header = {'HTTP_AUTHORIZATION': 'Token {}'.format(self.token)}
          user_obj = User.objects.get(pk=self.user.pk)
          request = self.client.post(reverse('message-list'), data=self.data, **header)
          response = MessageListView.as_view()(request)
          self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  
      def test_anonymous_cant_create_message(self):
          request = self.client.post(reverse('message-list'), data=self.data)
          response = MessageListView.as_view()(request)
  
          self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
  </code></pre>
  
  <p>Traceback</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/bkovacev/PycharmProjects/silver_logic_project/posts/tests.py", line 28, in test_loggedin_user_can_create_message
      response = MessageListView.as_view()(request)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
      return view_func(*args, **kwargs)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
      return self.dispatch(request, *args, **kwargs)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/views.py", line 477, in dispatch
      response = self.handle_exception(exc)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/views.py", line 437, in handle_exception
      self.raise_uncaught_exception(exc)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/views.py", line 474, in dispatch
      response = handler(request, *args, **kwargs)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/generics.py", line 243, in post
      return self.create(request, *args, **kwargs)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 21, in create
      self.perform_create(serializer)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 26, in perform_create
      serializer.save()
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 214, in save
      self.instance = self.create(validated_data)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 902, in create
      instance = ModelClass.objects.create(**validated_data)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
      return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 397, in create
      obj = self.model(**kwargs)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/django/db/models/base.py", line 537, in __init__
      setattr(self, field.name, rel_obj)
    File "/home/bkovacev/envs/silver_logic_project_env/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 211, in __set__
      self.field.remote_field.model._meta.object_name,
  ValueError: Cannot assign "u'johnz'": "Message.author" must be a "User" instance.
  </code></pre>
  
  <p>I have read other threads similar to this one, checked whether I'm referencing the right User Model as well and I have tried numerous things, but without luck.</p>
  
  <p>What am I doing wrong? If I add <code>format='json'</code> to the <code>self.client.post()</code> I get <code>&lt;User: john&gt; is not JSON serializable</code>. So I am not sure what I am doing wrong.</p>
  
  <p>EDIT:</p>
  
  <p>Added <code>models.py</code></p>
  
  <pre><code>from __future__ import unicode_literals
  from django.db import models
  from django.contrib.auth.models import User
  from django.contrib.postgres.fields import JSONField
  
  
  class Message(models.Model):
      """
  
      """
      author = models.ForeignKey(
          User, on_delete=models.CASCADE, related_name='messages'
      )
      created_at = models.DateTimeField(auto_now_add=True)
      body = models.TextField(max_length=160)
      sentiment = JSONField(blank=True, null=True)
  
      def __str__(self):
          return "%s, %s, %s" % (self.author, self.created_at, self.id)
  </code></pre>
  
  <p>EDIT 2 (added serializers and views)
  EDIT 3 (adapted the serializer)
  <code>serializers.py</code></p>
  
  <pre><code>class MessageSerializer(serializers.HyperlinkedModelSerializer):
  
      author = UserSerializer()
  
      class Meta:
          model = Message
          fields = ('author', 'sentiment', 'created_at', 'body', 'url')
  </code></pre>
  
  <p><code>views.py</code></p>
  
  <pre><code>class MessageListView(generics.ListCreateAPIView):
      """
      Class Based View that lists/creates Silver Logic(s)
      """
      queryset = Message.objects.all()
      serializer_class = MessageSerializer
      permission_classes = (IsAuthenticatedOrReadOnly, )
      pagination_class = PageNumberPagination
  </code></pre>