title: Django Rest Framework Unit Testing
body: <p>I there, so i am trying to use DRF Unit testing and i am having some problems with the <code>.post</code></p>
  
  <p>I think it has something to do with the foreign keys i am using but i couldn't find any good examples.</p>
  
  <p>Serializer:</p>
  
  <pre><code>class UserSerializer(serializers.ModelSerializer):
      class Meta:
          model = get_user_model()
          fields = ('password', 'id', 'username', 'email')
          write_only_fields = ('password',)
  
      def restore_object(self, attrs, instance=None):
          # call set_password on user object. Without this
          # the password will be stored in plain text.
          user = super(UserSerializer, self).restore_object(attrs, instance)
          user.set_password(attrs['password'])
          return user
  
  class MultimediaSerializer(serializers.ModelSerializer):
      class Meta:
          model = Multimedia
  
  
  class SpaceSerializer(serializers.ModelSerializer):
      user = UserSerializer()
      avatar = serializers.HyperlinkedRelatedField(view_name='multimedia-detail')
      contents = serializers.HyperlinkedRelatedField(
          many=True, view_name='multimedia-detail'
      )
      api_key = serializers.SerializerMethodField('get_api_token')
  
      class Meta:
          model = Space
  </code></pre>
  
  <p>models:</p>
  
  <pre><code>class UserProfile(models.Model):
      user = models.OneToOneField(settings.AUTH_USER_MODEL)
      avatar = models.ForeignKey(
          'core.Multimedia', blank=True, null=True,
          related_name='user_profiles_avatares'
      )
      language = models.ForeignKey('core.Language', blank=True, null=True)
      birth_date = models.DateTimeField(blank=True, null=True)
      country = CountryField(blank=True, default='PT')
      about_me = models.TextField(blank=True, default='')
      facebook_token = models.TextField(blank=True, default='')
      space_themed_motivation = models.TextField(blank=True, default='')
      created_on = models.DateTimeField(auto_now_add=True)
      updated_on = models.DateTimeField(auto_now=True)
      last_login_on = models.DateTimeField(auto_now=True)
  
  
  class Space(UserProfile):
      degree = models.CharField(max_length=200, blank=True)
      galleries = models.ManyToManyField('core.Gallery', blank=True, null=True)
      contents = models.ManyToManyField('core.Multimedia', blank=True, null=True)
  </code></pre>
  
  <p>So my body data that i use in this endpoint is:</p>
  
  <pre><code>def test_register_space(self):
      url = reverse('space-list')
      data = {
        "user": {
          "username": "blaya2", 
          "email": "blaya2@example.com",
          "password": "blayablaya" 
        },
        "avatar": "http://localhost:8000/api/v1/multimedia/1/",
        "contents": [
          "http://localhost:8000/api/v1/multimedia/1/",
          "http://localhost:8000/api/v1/multimedia/2/" 
        ],
        "language": "PT",
        "birth_date": "2014-10-30T10:59:22Z",
        "country": "PT",
        "about_me": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
        "facebook_token": "CAALDUez5gFsBAHGZCsi1BOeKwc",
        "space_themed_motivation": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
        "created_on": "2014-10-30T10:59:30.556Z",
        "updated_on": "2014-10-30T11:30:00.717Z",
        "last_login_on": "2014-10-30T11:30:00.717Z",
        "degree": "Mestrado" 
      }
  
      response = self.client.post(url, data, format='json')
      print response.data
  </code></pre>
  
  <p>and then i call it using <code>response = self.client.post(url, data, content_type='application/json')</code>
  I have also tried json.dumps() with no luck. I am always getting a <code>400</code> status code.</p>
  
  <p>when i do <code>print response.data</code> i get:</p>
  
  <pre><code>{'language': [u"Invalid pk 'PT' - object does not exist."], 'user': [{u'non_field_errors': [u'Invalid data']}], 'contents': [u'Invalid hyperlink - object does not exist.'], 'avatar': [u'Invalid hyperlink - object does not exist.']}
  </code></pre>
  
  <p>I understand i must have the links to point to, but i dont know the syntax to get this working.</p>
  
  <p>Does anyone know how i can get this to work?</p>