Django Rest Framework Image with Bad Request with Axios and Expo

I am trying to upload an image to my Django APÌ through a form in expo, but I keep getting an error 400. it is for a social feed style post with image and text.

This is the model:

class Post(models.Model):
    user = models.ForeignKey(User, related_name="post_creator", on_delete=models.CASCADE)
    text = models.CharField(max_length=900, blank = True, null = True)
    image = models.ImageField(blank = True, null = True, upload_to = post_directory_path)
    likes = models.ManyToManyField(User, blank = True)
    comments = models.ManyToManyField(Comment, blank = True)

    def __str__(self):
        return f"{self.user}: {self.text}"

The serializer:

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = "__all__"

Views.py: (permission_classes is commented for testing purposes)

class PostList(generics.ListCreateAPIView):
    # permission_classes = (IsAuthenticated, )
    serializer_class = PostSerializer
    queryset = Post.objects.all()
    parser_classes = (MultiPartParser, FormParser)

this is my function to the post itself, in Expo:

const postData = async () => {

    try {
      const formData = new FormData()
      formData.append('image', image)
      formData.append('user', 2) // 2 here is just for testing purposes
      formData.append('text', text)
      console.log(formData)
      const resp = await authAxios.post('posts/', formData, {
        headers: {
            "Content-Type": "multipart/form-data",
        }})
      console.log(resp.data)
      setText('')
      setImage(null)
    } catch (error) {
      console.error(error)
    } 

  }

I tried a lot of stuff, but no success, I am using expo-image-picker. Thank you!

Back to Top