当用户只想更改其文章的其他属性时,代码也会强制用户更改图像

The code is forcing the user to change the image also when he want to change only other properties of his article

大家好我有一个博客平台,用户可以添加和编辑博客。我使用 Django 和 Django Rest Framework 来构建网站,并用于前端 ReactJS。我的问题是当我编辑博客时,我无法保存更改,直到我更改图像。

在 React 中 edit.js

const handleSubmit = (e) => {
    e.preventDefault();
    console.log(formData);

    let ss = new FormData();
    ss.append('title', formData.title);
    ss.append('slug', formData.slug);
    ss.append('content', formData.content);
    ss.append('image', postimage.image[0]);

    axiosInstance.patch(`admin/edit/`+ id + '/', ss,  {
        headers:{
            Authorization: `JWT ${localStorage.getItem('refresh_token')}`
    },
    },)
    
};

任何提示或指导都会对我完成项目真正有帮助,因为我已经坚持了两个多星期。

谢谢

问题出在 handleSubmit,如果 postimage 为 null,则无法读取该值:

const handleSubmit = (e) => {
    e.preventDefault();
    console.log(formData);

    let ss = new FormData();
    ss.append('title', formData.title);
    ss.append('slug', formData.slug);
    ss.append('content', formData.content);

    if (postimage != null) {
       ss.append('image', postimage.image[0]);
    }

    axiosInstance.patch(`admin/edit/`+ id + '/', ss,  {
        headers:{
            Authorization: `JWT ${localStorage.getItem('refresh_token')}`
    },
    },)
    .then((res) => {
        // navigate('/admin/');
    });
    
};

记得为BlogSerializerimage字段设置required=False