I have a model Fixture
Class Fixture(models.Model):
event_timestamp = models.DateTimeField(null=True)
I have piece of json data
for item in piece_json:
event_timestamp = item["event_date"]
Where in item["event_timestamp"] the following data 1572567600
While i am trying to create object from this model
fixture = Fixture.objects.create(event_timestamp= event_timestamp)
I got the following error
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
After this error i tried to wrap event_timestamp variable in built-in str() function
fixture = Fixture.objects.create(event_timestamp= str(event_timestamp))
After it i got error
django.core.exceptions.ValidationError: ["'1572651000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
I don't think DateTimeField allowsyou to save Unix Timestamp.
What I can suggest you do is:
import datetime
fixture = Fixture.objects.create(event_timestamp=datetime.datetime.utcfromtimestamp(event_timestamp))
Related
Having an issue grabbing the submission.created_utc value and trying to store it in my django models.DateTimeField.
// here's my model's DateTimeField
red_created = models.DateTimeField(default=timezone.now, editable=False)
// this is how I try to save it
red_created = returnUtcDate(submission.created_utc)
// value of created_utc = 1589720741.0
def returnUtcDate(utcDate):
dt = datetime.fromtimestamp(int(utcDate))
newUtcDate = timezone.make_aware(dt, timezone.utc)
return newUtcDate // value being returned 2020-05-17 13:05:41+00:00
When I try to run that I get:
TypeError: expected string or bytes-like object
I know I need to convert the utc date before I store it but I don't know how or to what format. Tried searching but I'm assuming searching for the wrong terms.
This is mym models:
class Post(models.Model):
timestamp = models.DateTimeField()
I am trying to populate this field like this:
Post.objects.create(timestamp='20-03-20 8:56')
and throws me following error:
django.core.exceptions.ValidationError: ['“20-03-20 8:56” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']
Can anyone help me to fix this?
As specified, the format is:
YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
Hence you can construct this with:
Post.objects.create(timestamp='2020-03-20 8:56')
It might however be more convenient to pass a datetime object:
from datetime import datetime
from pytz import UTC
Post.objects.create(timestamp=datetime(2020, 3, 20, 8, 56, tzinfo=UTC))
You can of course select another timezone instead.
I have a model with a DateField field with blank = True and null = True.
It seems that is not possibile save an object with an empty value for the Datefield.
class ProjectTask(models.Model):
actual_start_date = models.DateField(blank=True, null=True)
Then try with Python console:
p = ProjectTask.objects.all().last()
p.actual_start_date = ""
p.save()
The results is:
venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1273, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'' value has an invalid date format. It must be in YYYY-MM-DD format."]
I'm using Python 3.4, Django 1.9 and PostgreSQL.
Any suggestions?
Dates are not strings. If you want to pass a string, it needs to be converted to a date. Normally, your form would validate whether or not you had a valid date, but you've bypassed this by setting an empty string directly on the model.
If you don't want to set a value at all, then pass None.
I have an optional datefield in my form, when I save it and it's empty I got the validation error about the invalid format.
In my model, I wrote blank=True and null=True.
Model :
created = models.DateTimeField(blank=True,null=True)
Form :
class XxxForm(forms.ModelForm):
created=forms.DateField(required=False)
class Meta:
model = Xxx
Error :
u"" value has an invalid format. It must be in YYYY-MM-DD HH:MM format
Update :
I found the solution which is :
created = request.POST['created']
if not created:
created = None
It works that way ! thanks everyone
You need to use a DateTimeField in the form, not a DateField.
[EDIT]
Also try to drop created=forms.DateField(required=False) from your form declaration. It is not needed there since you have it in the model already.
My model is:
class Report(models.Model):
created = models.DateTimeField(auto_now_add=True)
My views is:
def filter_range(request):
results = Report.objects.filter(created__range=['2012/01/09 00:00','2012/01/11 23:59'])
return render_to_response("log/filter.html", {'results':results})
It gives the error:
Exception Type: ValidationError
Exception Value: [u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']
What could be wrong? Thank you.
have you read the error message?
2012/01/09 is not in the formt YYYY-MM-DD
(slashes aren't dashes)