0

I have a form which has to do multiple file uploads at once, through Django ModelForm.

The scenario is like, I have a form in which different categories of documents has to be attached. For example, there maybe Medical records, Physician referrals, Diagnostic reports, etc. Each of these categories are stored in the DB. I have to upload all these documents according to their respective categories, through a single form submission.

Here are my Django models.

class Patient(models.Model):
    first_name = models.CharField(_("First Name"), max_length=30)
    last_name = models.CharField(_("Last Name"), max_length=30)
    email = models.EmailField(max_length=255)

class DocumentType(models.Model):
    """
    Type of document to be attached alongside a Patient.
    """
    name = models.CharField(max_length=255)

class DocumentFile(BaseModel):
    """
    Documents to be attached alongside each document type within a 
    Patient Case. Multiple documents to be attached.
    """
    type = models.ForeignKey(DocumentType)
    attachment = models.FileField(upload_to=get_new_documents_path, blank=True)
    patient = models.ForeignKey(Patient, related_name="get_applicable_documents")

Please tell me which is the best method to process the form submission. Any help would be much appreciated.

Jibu James
  • 1,059
  • 12
  • 9

1 Answers1

0

I've been working on a Django project and also have to submit forms that contain file uploads. I don't know how exactly you interact with your users, but I can explain my case and hope it helps and has some relatability to yours.

In my case I use the FormData interface ("multipart") to submit my forms via the Web, performing POSTs to the server. My forms sometimes contain only simple fields, and other times may contain various File Uploads. You can use the FormData interface and append multiple fields to an object, and then send it to your Django Server via POSTing. In my case, I'm also using it with JQuery AJAX asynchronously, though that's not a requirement.

You can find the basic idea for this at W3Schools, and you can try some javascript examples here. Also, if you have any asynchronous constraints, AJAX, there's a good starting point here on Stack Overflow.

Good luck

Community
  • 1
  • 1
S_alj
  • 407
  • 1
  • 3
  • 13