我正在使用ReportLab创建一个PDF,我想从订单模型中汲取单个字段(该字段称为'remote'(。我目前正在使用orders.objects.all((,但是我不确定如何参考模型的特定字段。
我目前拥有的:
def write_pdf_view(request):
doc = SimpleDocTemplate("/tmp/somefilename.pdf")
styles = getSampleStyleSheet()
Story = [Spacer(1,2*inch)]
style = styles["Normal"]
orders = Orders.objects.all()
for i in orders:
bogustext = ("This is Order number %s. " %i)
p = Paragraph(bogustext, style)
Story.append(p)
Story.append(Spacer(1,0.2*inch))
doc.build(Story)
fs = FileSystemStorage("/tmp")
with fs.open("somefilename.pdf") as pdf:
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
return response
return response
这将返回带有文本"这是订单号订单对象"的PDF,但我希望它读为"这是订单号参考" "
我认为您可以像这样
访问它bogustext = ("This is Order number %s. " % i.reference)
在您的型号
class Order(db.Model):
...
def __str__(self):
return self.reference