0

I have a model similar to

class MyModel(models.Model):
    startTime = models.DateTimeField(db_column='start_time', blank=False, default=timezone.now)

Everything is working as expected. However, if I change the date from admin panel, then the date format is not consistent to the original format and it strips the microseconds. just like the below

From "startTime": "2020-12-25T04:50:26.173085Z" to "startTime": "2021-03-08T01:50:19Z"

How can I still save the data from admin panel and save the original format with microseconds?

I am using MySQL as my database.

Thanks in Advance.

sadat
  • 4,004
  • 2
  • 29
  • 49
  • Does this answer your question? [Format for DateTimeField](https://stackoverflow.com/questions/1945239/format-for-datetimefield) – Anvesh Mar 08 '21 at 08:42
  • What exactly is `entity.startTime` and `entity2.endTime`? It's `datetime.datetime` and then something seems to be serializing it to a different format. That's the culprit. Show your serializers and these two datetime objects. – Tom Wojcik Mar 08 '21 at 10:28
  • @Anvesh I have edited the question to make it more specific. – sadat Mar 08 '21 at 12:41
  • @TomWojcik this item is not even in serializer. I am using the default behaviour for this field. – sadat Mar 08 '21 at 12:42
  • @sadat It is not the issue with saving I believe, but how the input is provided to the model, check your form field, to what extent input datetime microseconds is handled. – Anvesh Mar 08 '21 at 13:00
  • @sadat no, default type of `models.DateTimeField` is not str, so you have to do __something__ with it. – Tom Wojcik Mar 08 '21 at 13:08
  • @Anvesh I have not modified the field anywhere in my code. Its perfectly working with REST calls. but just changing the format from admin panel where I have not customized anything. – sadat Mar 08 '21 at 13:08
  • @TomWojcik I dont have problem with the default format. the problem is it strips the microseconds when the model is saved from admin panel. – sadat Mar 08 '21 at 13:11
  • @sadat You can control the format from admin interface too, that shouldnt be a problem. in `def clean` method of admin form – Anvesh Mar 08 '21 at 13:32
  • @Anvesh is it possible for you to answer the question? I am really not a django developer..I am still learning. Thanks – sadat Mar 09 '21 at 02:55

0 Answers0