0

This is my views.py:

class UpdateListMyShipmentView(UpdateView):
    model = models.Shipment
    fields = []

    def get_success_url(self):
        return reverse_lazy('request:my_shipment')

    def form_valid(self, form):
        print(timezone.localtime(timezone.now()))
        form.instance.time_finished_shipment = timezone.localtime(timezone.now())
        form.save()
        return super().form_valid(form)

This is the value of print() method in command line:

2018-06-15 16:54:29.499707+07:00

And this is the result in database after saved value into database:

2018-06-15 09:43:39.025761+00

Why it is different?

Quoc Huy
  • 9
  • 5

1 Answers1

0

Your database timezone and project timezone are different. That explains the +00 vs +07:00 difference... but the difference in minutes ( 43.36 vs 54.29 ) i guess it is just a copying error?

Odif Yltsaeb
  • 5,575
  • 12
  • 49
  • 80
  • Can you talk more detail or give me a keyword to solve issue above, pls? – Quoc Huy Jun 15 '18 at 17:20
  • @QuocHuy if you wish you can fix the problem on django side by setting the timezone value to the same that database has: https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/#default-time-zone-and-current-time-zone. Do that if database is running UTC timezone (if project is global) or when database uses your local timezone (if project is local). Otherwise do the change in postgres. In any case: https://stackoverflow.com/questions/12675723/changing-time-zone-value-of-data – Odif Yltsaeb Jun 19 '18 at 08:19