I have a charting library which requires my data in the epoch/Unix timestamp timestamp.
So I convert it like this:
ydm = 20170101
ymd = ms['dimensions'][0] + '-' + '235959'
d = datetime.datetime.strptime(ymd,'%Y%m%d-%H%M%S')
date = d.strftime('%Y-%m-%d')
ts = int(d.strftime("%s")) * 1000
This works fine and as a result I get a human readable field 2017-04-08
and I timestamp 1491695999000
I'm trying to save both fields in my db, I first tried with a IntegerField
, but I was getting the above error, I thought that it was because of the timestamp being such a big number so I also tried a BigIntegerField
but I keep getting the error with a random(?) number near like: invalid literal for int() with base 10: '100.0'
The number has always a dot and I guess that's why django rightly is not accepting it.
My view looks something like this:
print('time stamp is: '+str(ts)) # This is an int and the result is something like this: `time stamp is: 1491868799000`
# Save Metrics to DB
gp = GoogleProperty.objects.get(google_email=current_user)
user_id = int(gp.user_id)
property_id = gp.property_id
property_name = gp.property_name
obj, created = GaCountries.objects.get_or_create(property_id=property_id, date=date, country=country,
defaults={
'owner':user_id,
'property_id':property_id,
'date':date,
'property_name':property_name, # Remove Later
'country':country,
'sessions' : sessions,
'users' : users,
'pageviews' : pageviews,
'bouncerate': bouncerate,
'timestamp' : ts,
})
if not created: # If exists which data should be replaced:
obj.date = date
---ETC
The problem occurs in the line: 'timestamp' : ts,
but I'm sure ts is an integer and the data is correct.
The model now is:
timestamp = models.BigIntegerField(blank=True, null=True)
But I tried also with other types of fields...
Any ideas?
Santiago Trujillo