My attempt at making a pdf fails with a server500 error when I attempt to export a field that is created by a foreign key. I believe this to somehow be the problem, as all other fields work as expected. This same field can be similarly exported as a csv.
in views.py:
def report_pdf(request):
# Create a Bytestream buffer
buf = io.BytesIO()
# Create a canvas
c = canvas.Canvas(buf, pagesize=letter, bottomup=0)
# Create a text object
textob = c.beginText()
textob.setTextOrigin(inch, inch)
textob.setFont("Helvetica", 14)
# Designate the model
customers = Customer.objects.all()
lines =[]
for customer in customers:
lines.append(customer.address)
lines.append(customer.address2)
lines.append(customer.city)
lines.append(customer.province3)
# Loop
for line in lines:
textob.textLine(line)
# Finsih up
c.drawText(textob)
c.showPage()
c.save()
buf.seek(0)
return FileResponse(buf, as_attachment=True, filename='customer_list.pdf')
and in models.py
class Customer(models.Model):
address = models.CharField(max_length=100, null=True, blank=True)
address2 = models.CharField(max_length=100, null=True, blank=True)
city = models.CharField(max_length=50, null=True, blank=True)
province3 = models.ForeignKey(Province, null=True, blank=True, on_delete=models.PROTECT)
and the foreign model:
class Province(models.Model):
province = models.CharField(max_length=2)
def __str__(self):
return(f"{self.province}")
I’ve tried disabling the province3 field, at which point it works.
Am I correct that it has to do with the fact that it uses a foreign key? If so, what may be the problem? I have checked the site logs and the apache logs but there doesn’t seem to be any help there.
1