1

is there any way to make choices dynamically depend on current user something like that:

class RideSerializer(serializers.ModelSerializer):
    provider_username = serializers.ChoiceField(choices=request.user.providers)
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 06 '22 at 14:58

1 Answers1

2

You can set the choices during in the constructor. The following code show the idea. You can pass the request via context https://www.django-rest-framework.org/api-guide/serializers/#including-extra-context

class RideSerializer(serializers.ModelSerializer):
    provider_username = serializers.ChoiceField()

    def __init__(self, *args, **kwargs):
       super().__init__(*args, **kwargs)
       self.fields["provider_username"].choices = request.user.providers
hendrikschneider
  • 1,696
  • 1
  • 14
  • 26