Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeZoneSerializerField not accepting choices argument #69

Open
gabn88 opened this issue Feb 28, 2021 · 5 comments
Open

TimeZoneSerializerField not accepting choices argument #69

gabn88 opened this issue Feb 28, 2021 · 5 comments

Comments

@gabn88
Copy link

gabn88 commented Feb 28, 2021

And as a result, the OPTIONS request does not show all the possible choices in the API.

This is related to #61

In my opinion, this should be the first step.

@gabn88
Copy link
Author

gabn88 commented Feb 28, 2021

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.

@mfogel
Copy link
Owner

mfogel commented Mar 15, 2021

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.

@hyshka
Copy link

hyshka commented Mar 19, 2021

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

@normic
Copy link

normic commented Sep 3, 2023

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?

@gabn88
Copy link
Author

gabn88 commented Dec 4, 2023

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants