Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1705

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:39:08+00:00 2024-11-25T10:39:08+00:00

How to return DateTime in different time zones from Django Rest Framework

  • 62k

This is how we can return DateTimes with dynamic time zones from DRF.

So, early this week I got a new ticket where I had to create new endpoints for our API, a few endpoints with DRF were not a big deal I said, this was until I realized that the DateTime values in the response from these endpoints needed to be converted from UTC to other time zones.

We always store and return our DateTime values in UTC, and we convert them to the time zone of the user at display time, this can be in the browser using JS. But this time was different, the API needed to return the values in a specific time zone.

We have something like this.

 class Venue(models.Model):     time_zone = models.CharField(max_length=128, default='UTC')     ...  class Event(models.Model):     start_time = models.DateTimeField()     end_time = models.DateTimeField()     venue = models.ForeignKey(Venue, on_delete=models.CASCADE)     ... 
Enter fullscreen mode Exit fullscreen mode

Each Event is happening in a Venue, this venue is placed in some city, so we need to store the time zone used for that venue. Since we want to sell tickets or just promote some events we need to return the DateTime values in the venue time zone so we don't confuse the customers and other resellers.

The first solution I thought of was the serializer field DateTimeField, it accepts an attribute called default_timezone, this is a pytz.timezone representing the timezone. If not specified and the USE_TZ setting is enabled, this defaults to the current timezone. If USE_TZ is disabled, then datetime objects will be naive.

class EventSerializer(serializers.ModelSerializer):     start_time = serializers.DateTimeField(default_timezone=pytz.timezone('America/Bogota'))     end_time = serializers.DateTimeField(default_timezone=pytz.timezone('America/Bogota'))     ... 
Enter fullscreen mode Exit fullscreen mode

This works, if the start_time in UTC is 2021-09-02T10:00:00 in the resultant JSON will be displayed as 2021-09-02T05:00:00-05:00. But it didn't just work for me because I don't know the time zones beforehand and also they will be different, so I needed to do this at runtime, my solution was to override the to_representation() method from the serializer.

class EventSerializer(serializers.ModelSerializer):     def to_representation(self, instance):         self.fields['start_time'] = serializers.DateTimeField(default_timezone=pytz.timezone(instance.venue.time_zone))         self.fields['end_time'] = serializers.DateTimeField(default_timezone=pytz.timezone(instance.venue.time_zone))         return super().to_representation(instance)     ... 
Enter fullscreen mode Exit fullscreen mode

to_representation is run for each instance that is going to be returned by the serializer, so basically what we are doing is overriding the start_time and end_time declaration for each instance, this is for specifying that we want to use a DateTimeField and we want to pass it a default_timezone which is going to be taken from the venue that is linked to the instance.

This helped me to do what I needed and I hope that this can help you as well.

You can follow me on Twitter and GitHub to be up to date with all my projects and content.

djangoprogrammingpythonwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.