最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How To Append & Send File data in a multipart react form with Django backend with Nested JSON - Stack Overflow

programmeradmin5浏览0评论

So, I have a form data with the corresponding JSON format

  project_status: 'OPEN',
    project_title: '',
    project_code: '',
    start_date: '',
    end_date: '',
    duration: '',
    engagement_year: '',
    financial_year: '',
    project_budget: '',
    domain: '',
    project_type: '',
    project_mou: null,
    other_docs: null,
    thirdAgencyDetails: {
      government_body: '',
      scheme_name: '',
      sanctioned_budget: '',
      industry_contribution: '',
      government_contribution: '',
      agreement: null,
      sanction_letter: null,
      proposal_doc: null
    },
    facultyDetails: [],

I trying to send this data to Django backend which has the following model

class Project(models.Model):
    user = models.CharField(max_length=15, null=True)
    project_title = models.CharField(max_length=250, null=True)
    project_code = models.CharField(max_length=250, null=True)
    start_date = models.DateField(max_length=10, null=True, default='')
    end_date = models.DateField(max_length=10, null=True, default='')
    duration = models.CharField(max_length=255)
    engagement_year = models.IntegerField()
    financial_year = models.CharField(max_length=255)
    project_budget = models.DecimalField(max_digits=10, decimal_places=2)
    domain = models.CharField(max_length=250, null=True)
    project_type = models.CharField(max_length=255)
    project_status=models.CharField(max_length=250, blank=True)
    thirdAgencyDetails = models.OneToOneField(ThirdAgencyDetail, on_delete=models.CASCADE, null=True, blank=True)
    project_mou = models.FileField(upload_to='R&D_docs/', blank=True)
    other_docs = models.FileField(upload_to='R&D_docs/', blank=True)

with a one to one field to

class ThirdAgencyDetail(models.Model):
    government_body = models.CharField(max_length=255)
    scheme_name = models.CharField(max_length=255)
    sanctioned_budget = models.DecimalField(max_digits=10, decimal_places=2)
    industry_contribution = models.DecimalField(max_digits=10, decimal_places=2)
    government_contribution = models.DecimalField(max_digits=10, decimal_places=2)
    agreement = models.FileField(upload_to='jointProject_thirdAgency_docs/',blank=True)
    sanction_letter = models.FileField(upload_to='jointProject_thirdAgency_docs/',blank=True)
    proposal_doc = models.FileField(upload_to='jointProject_thirdAgency_docs/',blank=True)

I have written a form uploading code refer code gist here
I am unable to append the files for 'thirdAgencyDetails' since it is a nested JSON in similar manner to

if (formData.project_mou) {
        formDataToSend.append('project_mou', formData.project_mou);
      }

What I have tried ?

      if(formData.thirdAgencyDetails){
        formDataToSend.append('agreement',formData.thirdAgencyDetails['agreement'])

      }
if(formData.thirdAgencyDetails){
        formDataToSend.append('agreement',formData.thirdAgencyDetails.agreement)

      }

but it always appends it to root JSON instead of nested JSON Giving error

response : "{"error":"Project() got unexpected keyword arguments: 'agreement'"}" responseText : "{"error":"Project() got unexpected keyword arguments: 'agreement'"}"

and even

  if(formData.thirdAgencyDetails){
        formDataToSend.append('thirdAgencyDetails' , {
          'agreement': formData.thirdAgencyDetails.agreement,
          'sanction_letter' : formData.thirdAgencyDetails.sanction_letter,
          'proposal_doc':formData.thirdAgencyDetails.proposal_doc
        })
      }

Even Tried this

if(formData.thirdAgencyDetails){
        formDataToSend.append('thirdAgencyDetails' , JSON.stringify(formData.thirdAgencyDetails))
      }

but the above just adds empty object instead of the object itself !!!

P.S : The line in gist code works properly only insertion to nested JSON doesn't work

if (formData.project_mou) {
        formDataToSend.append('project_mou', formData.project_mou);
      }

how to resolve this ?

Update : Added Django Code for the above.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论