title: Django REST Framework: Get ID/URL during validation?
body: <p>I have a <code>HyperlinkedModelSerializer</code>. To implement its <code>validate</code> method properly, I need to access the <em>primary key</em> or <em>the URL</em> of the object that is being validated – if it has one, i.e. if it's being <em>edited</em>, not created. What's the correct way of doing that?</p>
  
  <p>I tried many things, but the only approach that worked was a hack that fetches the ID of the object when the serializer is instantiated into it's <code>id</code> field:</p>
  
  <pre><code>class BoxSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = Box
          fields = ('id', 'name', 'url')
  
      def __init__(self, *args, **kwargs):
          super(BoxSerializer, self).__init__(*args, **kwargs)
          self.id = None \
              if len(args) != 1 or not isinstance(args[0], Box) \
              else args[0].id
  
      def validate(self, data):
          print(data)
          return data
  </code></pre>
  
  <p>What's the proper way to access the ID/URL of the object being validated from within the serializer's <code>validate</code> method? Neither <code>data['id']</code> nor <code>data['url']</code> exist.</p>
  
  <hr>
  
  <p><code>urls.py</code>:</p>
  
  <pre><code>urlpatterns = [
      url(r'(?P&lt;pk&gt;[0-9]+)/$', views.BoxDetail.as_view(), name='box-detail'),
  ]
  </code></pre>
  
  <p><code>views.py</code>:</p>
  
  <pre><code>class BoxDetail(generics.RetrieveUpdateDestroyAPIView):
      queryset = Box.objects.all()
      serializer_class = BoxSerializer
  </code></pre>