GXM100
GXM100

Reputation: 467

How to Create and Save File in Django Model

I have a view which generates a .docx file for whichever 'reference #' the user selects - I would like for the file to save to my Orders model whenever the doc is generated.

models.py

#model where I'd like to save the doc each time it gets generated
class Orders(models.Model):

    reference = models.CharField(max_length=50, unique=True, error_messages={'unique':"This reference id has already been used"})
    ultimate_consignee = models.ForeignKey(Customers, blank=True)
    ship_to = models.CharField(max_length=500, blank=True)
    vessel = models.CharField(max_length=100, blank=True)
    ...
    order_file = #not sure what data type to use here

views.py

#view generating the .docx
def docjawn(request):

    reference = request.POST.get('Reference_IDs')
    referenceid = reference
    manifest = Manifests.objects.all().filter(reference__reference=referenceid)
    order = Orders.objects.get(reference=reference)

    doc = DocxTemplate("template.docx")
    totalCNF = 0
    totalFOB = 0
    for item in manifest:
        totalCNF += item.cases * item.CNF
        totalFOB += item.cases * item.FOB
    context = {

        'ultimate_consignee' : order.ultimate_consignee,
        'reference' : order.reference,
        'ship_to' : order.ship_to,
        'terms' : order.terms,
        'date' : "12",
        'ship_date' : "7/4/19",
        'vessel' : order.vessel,
        'POE' : order.POE,
        'ETA' : order.ETA,
        'booking_no' : order.booking_no,
        'manifest' : manifest,
        'totalCNF' : totalCNF,
        'totalFOB' : totalFOB,
}


    doc.render(context)

    doc_io = io.BytesIO() # create a file-like object
    doc.save(doc_io) # save data to file-like object
    doc_io.seek(0) # go to the beginning of the file-like object

    response = HttpResponse(doc_io.read())

    # Content-Disposition header makes a file downloadable
    response["Content-Disposition"] = "attachment; filename=generated_doc.docx"

    # Set the appropriate Content-Type for docx file
    response["Content-Type"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

    return response

Ok so when the view above is accessed the system should generate the file and save it to the Orders model WHERE reference = reference_id. Is this possible? Any suggestions? I really have no idea how to start trying to connect the two.

Upvotes: 3

Views: 2942

Answers (1)

Mihai Chelaru
Mihai Chelaru

Reputation: 8262

It seems like your view is quite close to doing what you want. In your model, add a FileField for order_file:

class Orders(models.Model):

    ...
    order_file = models.FileField(upload_to='path/to/storage/', null=True, blank=True)

Then in your view, save the BytesIO object you've created to the order_file field in the Orders object with the correct reference_id by wrapping it in a File object:

from django.core.files import File

def docjawn(request):

    reference = request.POST.get('Reference_IDs')
    manifest = Manifests.objects.all().filter(reference__reference=reference)
    order = Orders.objects.get(reference=reference)

    # Generate doc file
    ...

    doc_io = io.BytesIO()
    doc.save(doc_io)
    doc_io.seek(0)

    # Save the BytesIO to the field here
    order.order_file.save("generated_doc.docx", File(doc_io))

    response = HttpResponse(doc_io.read())
    response["Content-Disposition"] = "attachment; filename=generated_doc.docx"
    response["Content-Type"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

    return response

Upvotes: 5

Related Questions