ReportLab - Python - Doubt with date writing (date)

0

I am having problems with reportlab.

Python 3 / Django application

I have a Pessoa model that contains a data de nascimento field that is of type DateTimeField . I am trying to write the date of birth in the PDF but the error. Remembering that the date comes from the database.

Other fields like Name, City, Address, etc. are writing in PDF correctly.

p = Pessoa.objects.get(pk=pk)
c.drawString(200, 630, p.data_nascimento)

I'musingReportLab( link )

    
asked by anonymous 26.10.2016 / 23:10

1 answer

2

datetime.datetime object has no attribute 'decode'

This error means that an object of class datetime.datetime is being called, somewhere, with the attribute / method decode .

The decode method, in Python3, is used by the bytes class to convert to string.

Follow the example:

>>> s = b'Oi'
>>> type(s)
<class 'bytes'>
>>> s = s.decode()
>>> s
'Oi'
>>> type(s)
<class 'str'>

To solve your problem, I believe the best way is to transform p.data_nascimento (a datetime.datetime class object) to bytes or str before sending to the drawString function. To do this, use strftime .

p = Pessoa.objects.get(pk=pk)
c.drawString(200, 630, p.data_nascimento.strftime('%d/%m/%Y'))
    
27.10.2016 / 01:13