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

Add the request to the context for the serializer #55

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

pjrobertson
Copy link

This is useful if subclassing the views and using custom serializers.

Use case: User Signups is restricted to administrator users only. I set up a custom SignupSerializer that needs to validate the email address to make sure the domain matches the current user - the current user needs to be found from request.user

E.g.:

def email_domain(email):
    # return email domain of 'email' - example.com

class EmailSignupSerializer(SignupSerializer):
    def validate_email(self, value):
        if email_domain(value) != email_domain(self.context['request'].user.email):
                raise serializers.ValidationError("You may only add users with from the same email domain as yours')
        return value

This is useful if subclassing the views and using custom serializers.

Use case: I set up a custom SignupSerializer that needs to validate the email address to make sure the domain matches the current user - the current user needs to be found from request.user
@pjrobertson
Copy link
Author

I just added one more commit that fixes #56

Allows django-rest to manage its own exceptions. Fixes celiao#56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants