I have a model Test
in service A
class Test(models.Model):
some_field = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True)
I am migrating data from a different service B
's model to this model (using APIs).
Exposed an API in service A
which reads from POST data sent from the B
, and saves the data in this Test
model.
Sample data coming from service B
data = {
'some_field': 5,
'created_at': '2013-06-26 00:14:27.260524',
}
Now when I save this data in the db using:
obj = Test(
some_field=request.POST.get('some_field'),
created_at=request.POST.get('created_at'),
)
obj.save()
The created_at
values of saved object is the current time. Not the created_at
coming from the API.
How to bypass this current time saving in this scenario and save the timestamp coming from the API while creating the object?
CodePudding user response:
When you have auto_now_add=True
you're forcing your records to have current time as their timestamp in the creation time and Django simply ignores this field in the creation moment if you try to pass a value to it (either on calling Test.objects.create()
or calling save
method on your object for the first time). I think you can handle this problem by doing two different solutions (there might be other solutions as well):
A- Try to change your field value from auto_now_add=True
to default=timezone.now
:
from django.utils import timezone
class Test(models.Model):
some_field = models.IntegerField()
created_at = models.DateTimeField(default=timezone.now)
And then you can pass a value for your created_time
in the creation process.
B- If this is a just one time import (or hitting your database multiple times does not have bad impact on your performance) you can try to update every instance after creation part:
obj = Test(some_field=request.POST.get('some_field'))
obj.save()
obj.created_at = request.POST.get('created_at')
obj.save(update_fields=['created_at'])
Note: Make sure that your request.POST.get('created_at')
is a valid datetime format, otherwise Django will raise an exception (It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format).