DRF dynamic choices depend on current user
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)
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