title: NOT NULL constraint failed: accounts_personalcolor.user_id
body: <p>I am new to Django and have trouble making django-rest-framework API for post, inheriting APIView. I'm using a serializer, that inherits djangos ModelSerializer. I face <strong>NOT NULL constraint failed: accounts_personalcolor.user_id</strong> error whenever I try saving the serializer or model object.</p>
  <p>color.js posts image using Django rest framework as follows.</p>
  <pre><code>function PersonalColorScreen({navigation,route}) {
      const {image} = route.params;
      console.log('uri is', image.uri);
      const [userToken, setUserToken] = React.useState(route.params?.userToken);
  
      const requestHeaders = {
          headers: {
              &quot;Content-Type&quot;: &quot;multipart/form-data&quot;
          }
      }
      // helper function: generate a new file from base64 String
      //convert base64 image data to file object to pass it onto imagefield of serializer.
      //otherwise, serializer outputs 500 Internal server error code
  
      const dataURLtoFile = (dataurl, filename) =&gt; {
      const arr = dataurl.split(',')
      const mime = arr[0].match(/:(.*?);/)[1]
      const bstr = atob(arr[1])
      let n = bstr.length
      const u8arr = new Uint8Array(n)
      while (n) {
        u8arr[n - 1] = bstr.charCodeAt(n - 1)
        n -= 1 // to make eslint happy
      }
      return new File([u8arr], filename, { type: mime })
    }
    //random number between 0-9
    function getRandomInt(max) {
      return Math.floor(Math.random() * max);
    }
    
    // generate file from base64 string
    const file = dataURLtoFile(image.uri, `${getRandomInt(10)}.png`)
    const formData= new FormData();
    formData.append('img',file,file.name);
  
    console.log(file.name);
      //axios post request to send data
      // axios.post('http://localhost:8000/accounts/personalcolor/',  formData,requestHeaders)
      //multipartparser
      axios.post('http://localhost:8000/accounts/personalcolor/', formData, requestHeaders)
      .then(res =&gt; {
          console.log(res);
          if (res.data === 'upload another image') {
              setimageError('upload another image');
          } else {
              // signUp(userToken);
              let color;
              switch (res.data){
                  case ('spring'):
                      color = 'spring';
                      break;
                  case ('summer'):
                      color = 'summer';
                      break;
                  case ('fall'):
                      color = 'fall';
                      break;
                  case ('winter'):
                      color = 'winter';
                      break;
              }
          }
          })
      .catch(err =&gt; {
          console.error(err.response.data)
      })
  </code></pre>
  <p>view.py handles the image posted. I tried #1 but it did not work. So I tried #2, or #3 instead and they return the same error saying <strong>NOT NULL constraint failed: accounts_personalcolor.user_id.</strong> I thought saving the serializer or model object creates id(Autofield)automatically and I don't understand why I face this error.</p>
  <p>views.py</p>
  <pre><code>@api_view(['POST'])
  def personalcolor(request):
  
      # 1
      image=request.FILES['img']
      personal_color=Personalcolor()
      personal_color.img=image
      personal_color.save()
  
      # 2
      image=request.FILES['img']
      personal_color=Personalcolor.objects.create(img=image)
      personal_color.save()
  
      # 3
      serializer = ColorSerializer(data=request.data)
      # validation of input data
      if serializer.is_valid():
          serializer.save()
      else:
          return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
  </code></pre>
  <p>model.py</p>
  <pre><code>class Personalcolor(models.Model):
      objects = models.Manager()
      img = models.ImageField('personal_img',upload_to=&quot;personalcolor/&quot;, blank=True)
  </code></pre>
  <p>serializer.py</p>
  <pre><code>class ColorSerializer(serializers.ModelSerializer):
     class Meta:
        model = Personalcolor
        fields = ['img']
  </code></pre>
  <p>As mentioned above, executing the code returns <strong>django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_personalcolor.user_id.</strong> Any help would be greatly appreciated.</p>