title: How to serialize data not coming from the request and properly validate it (ModelSerializer in Django Rest Framework)?
body: <p>Using Django Rest Framework 3, Function Based Views, and the ModelSerializer (more specifically the HyperlinkedModelSerializer).</p>
  
  <p>When a user submits a form from the client, I have a view that takes the request data, uses it to call to an external API, then uses the data from the external API to populate data for a model serializer.
   I <em>believe</em> I have this part working properly, and from what I read, you are supposed to use <strong>context</strong> and <strong>validate()</strong></p>
  
  <p>In my model serializer, I have so far just this one overidden function:</p>
  
  <pre><code>from django.core.validators import URLValidator
  
  def validate(self, data):
      if 'foo_url' in self.context:
          data['foo_url'] = self.context['foo_url']
          URLValidator(data['foo_url'])
      if 'bar_url' in self.context:
          data['bar_url'] = self.context['bar_url']
          URLValidator(data['bar_url'])
      return super(SomeSerializer, self).validate(data)
  </code></pre>
  
  <p>Just in case, the relevant view code is like so:</p>
  
  <pre><code>context = {'request': request}
  ...
  context['foo_url'] = foo_url
  context['bar_url'] = bar_url
  s = SomeSerializer(data=request.data, context=context)
  if s.is_valid():
      s.save(user=request.user)
      return Response(s.data, status=status.HTTP_201_CREATED)
  </code></pre>
  
  <p>Now assuming I have the right idea going (my model does populate its <code>foo_url</code> and <code>bar_url</code> fields from the corresponding context data), where I get confused is how the validation is not working.  If I give it bad data, the model serializer does not reject it.</p>
  
  <p>I assumed that in <code>validate()</code>, by adding the context data to the data, the data would be checked for validity when <code>is_valid()</code> was called.  Maybe not the case, especially when I print out <code>s</code> (after using the serializer but before calling <code>is_valid()</code>) there is no indication that the request object's <code>data</code> has been populated with the context data from <code>validate()</code> (I don't know if it should be).</p>
  
  <p>So I tried calling the URLValidators directly in the <code>validate()</code> method, but still doesn't seem to be working.  No errors despite giving it invalid data like 'asdf' or an empty python dict ({}).  My test assertions show that the field indeed contains invalid data like '{}'.</p>
  
  <p>What would be the proper way to do this?</p>