GithubHelp home page GithubHelp logo

Comments (5)

hyshka avatar hyshka commented on May 19, 2024 1

Thanks for this. I was using a different implementation with v4.0 which broke when I upgraded to v4.1.2. This fixed it for me.

image

from django-timezone-field.

gabn88 avatar gabn88 commented on May 19, 2024
class TimeZoneSerializerField(serializers.ChoiceField):
    def __init__(self, **kwargs):
        super().__init__(TimeZoneField.default_choices, **kwargs)

    def to_representation(self, value):
        return str(super().to_representation(value))

This is how I do it now. Maybe this could be incorporated into this package. It now shows a list of all possible timezone fields in the browsable API and in the in an OPTIONS request.

from django-timezone-field.

mfogel avatar mfogel commented on May 19, 2024

Agreed it would be great to respond to an OPTIONS request with the list of possible choices. I didn't realize that was provided by django rest framework choice field.

@gabn88 would it be possible to expand the field class you have there to accept an optional choices argument? The idea would be to support the case where someone wants the serializer field to only display a subset of all possible timezone choices.

I think if that was added to this implementation, and then also some basic tests, we could merge it in named something like TimeZoneSerializerChoiceField. Then it could live in parallel with the currently-existing TimeZoneSerializerField which we would need to keep around for backwards-compatibility till at least the next major version bump.

from django-timezone-field.

normic avatar normic commented on May 19, 2024
class TimeZoneSerializerField(serializers.ChoiceField):
    def __init__(self, **kwargs):
        super().__init__(TimeZoneField.default_choices, **kwargs)

    def to_representation(self, value):
        return str(super().to_representation(value))

This is how I do it now. Maybe this could be incorporated into this package. It now shows a list of all possible timezone fields in the browsable API and in the in an OPTIONS request.

@gabn88 How are you dealing with this now? As far as I've seen, there is no .default_choices anymore. Or do you stick with an old version?

from django-timezone-field.

gabn88 avatar gabn88 commented on May 19, 2024

@normic I use it like this

class TimeZoneSerializerChoiceField(TimeZoneSerializerField, serializers.ChoiceField):
    def __init__(self, **kwargs):
        self.use_pytz = kwargs.pop("use_pytz", None)
        self.tz_backend = get_tz_backend(use_pytz=self.use_pytz)
        super().__init__([(tz, tz) for tz in self.tz_backend.base_tzstrs], **kwargs)

from django-timezone-field.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.