Quick note: this error may be somewhat linked to this thread, but the use case and python version (the other one is still at v2) is different. Other similar threads don't regard specifically to python datetime
.
I have the following code:
import datetime
from .models import RaceData, RacerData
@periodic_task(crontab(minute='*/15'))
def record_racedata():
team = nitrotype.Team('PR2W')
timestamp = datetime.datetime.now() # timestamp
for members in team.data["members"]:
rcd = RaceData(
timestamp=timestamp,
)
rcd.save()
@periodic_task(crontab(minute='*/15'))
def record_racerdata():
timestamp = datetime.datetime.now() # timestamp
for members in team.data["members"]:
rcrd = RacerData(
timestamp=timestamp,
)
rcrd.save() # error comes from here
models:
class RaceData(models.Model):
id = models.BigAutoField(primary_key=True)
timestamp = UnixDateTimeField()
class RacerData(models.Model):
id = models.BigAutoField(primary_key=True)
timestamp = UnixDateTimeField()
And I get the following output:
AttributeError: 'int' object has no attribute 'timetuple'
What confuses me the most is how the first inclusion of timestamp
doesn't come with an error, but maybe the execution doesn't go from top to bottom.
Either way, I believe that I've initialized the timestamp
variable appropriately. Can anyone please help? Note that I cannot define timestamp
outside either function because I need the timestamp to be constantly updated with the periodically-called functions.
EDIT I've seen elsewhere that this error occurred because the namespace datetime
was used elsewhere in the code, but this is not the case in my question.
Thanks in advance.
This seems like a problem with the dependency that I'm using (
UnixDateTimeField
) as the error thrown out was pointed toward the package in my virtual environment.I resorted to store the time in an integer field:
models:
Any solution is still welcome, but after browsing several other questions on SO, I came to the solution that it's the simplest to store a UNIX timestamp as an integer. You can always convert an integer back to
datetime
as described here, assuming that the initial UNIX timestamp is validated.Still, thanks to everyone that tried to answer the question.