title: How to validate against full model data in Django REST Framework
body: <h2>TL;DR</h2>
  <p>In my <code>Serializer.validate</code> method, I need to be able to access <code>attrs['field']</code> and fall back to <code>self.instance.field</code> if it's not set in the incoming data, and I'm wondering if there is a common pattern to do so.</p>
  <h2>The Problem</h2>
  <p>Take the example from the <a href="https://www.django-rest-framework.org/api-guide/serializers/#object-level-validation" rel="nofollow noreferrer">Object-level validation</a> section of the DRF Serializers documentation:</p>
  <pre class="lang-py prettyprint-override"><code>from rest_framework import serializers
  
  class EventSerializer(serializers.Serializer):
      description = serializers.CharField(max_length=100)
      start = serializers.DateTimeField()
      finish = serializers.DateTimeField()
  
      def validate(self, attrs):
          &quot;&quot;&quot;
          Check that start is before finish.
          &quot;&quot;&quot;
          if attrs['start'] &gt; attrs['finish']:
              raise serializers.ValidationError(&quot;finish must occur after start&quot;)
          return attrs
  </code></pre>
  <p>(This uses a normal <code>Serializer</code>, but imagine it's a <code>ModelSerializer</code> for an <code>Event</code> model.)</p>
  <p>When an event is created, or updated where both the <code>start</code> and <code>finish</code> attributes are included in the data, then this serializer works as expected.</p>
  <p>However, if you make a request such as:</p>
  <pre class="lang-py prettyprint-override"><code>client.patch(f&quot;/events/{event.id}&quot;, {&quot;start&quot;: &quot;2021-01-01&quot;})
  </code></pre>
  <p>Then the serializer will fail, because trying to access <code>attrs['finish']</code> results in a <code>KeyError</code>. In this case, I need to be able fall back to <code>self.instance.finish</code>, since the <code>start &lt; finish</code> validation is still necessary.</p>
  <p>Is there a common pattern that solves this problem?</p>
  <h2>Current Solution</h2>
  <p>You can solve this by adding a snippet to the start of all <code>validate</code> methods:</p>
  <pre class="lang-py prettyprint-override"><code>def validate(self, attrs):
      full_attrs = attrs
      if self.instance is not None:
          full_attrs = {
              **self.to_internal_value(self.__class__(self.instance).data),
              **full_attrs,
          }
  </code></pre>
  <p>Then use <code>full_attrs</code> in place of <code>attrs</code>. This adds the serialized version of the instance data to <code>attrs</code>.</p>
  <p>Is there a better way to accomplish this?</p>
  <p>(The one &quot;downside&quot; with this is that it could prevent otherwise valid updates if the data loses it's integrity. So for instance, if a developer updates the database directly so that <code>event.start</code> is later than <code>event.finish</code>, an API user will no longer be able to update <code>event.description</code> since this validation would fail. But I think the pros definitely outweigh the cons, at least for my current use case.)</p>