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 write_only_fields support #1306

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants
@kevmo314

I was surprised to find that there wasn't any built-in support for write-only fields. I needed a field that I could validate, but didn't store or read from. For instance, when taking a password confirmation field. I put it in serializers.Serializer instead of serializers.ModelSerializer because this can be used completely independent of any model as well.

Thus, I added the functionality. It doesn't pass the field on to the model when saving (obviously), but it permits you to use validate_<x> functions. Example usage:

class ProfileSerializer(serializers.Serializer):
    class Meta:
        write_only_fields = ('current_password','password')
    email = serializers.CharField(required=False)
    password = serializers.CharField(required=False)
    current_password = serializers.CharField()

    def validate_current_password(self, attrs, source):
        if self.object is None:
            return attrs
        u = authenticate(username=self.object.user.email, password=attrs[source])
        if u is not None:
            return attrs
        else:
            raise serializers.ValidationError('Error!')
@tomchristie

This comment has been minimized.

Show comment
Hide comment
@tomchristie

tomchristie Dec 21, 2013

Member

Hiya @kevmo314, thanks for the input.

Write-only fields have come up before, see https://groups.google.com/d/msg/django-rest-framework/UxWRbNE9NyU/s38xVmh6sw0J and https://groups.google.com/d/msg/django-rest-framework/abMsDCYbBRg/_T0zoHcDonIJ although I'm not sure if your behaviour (don't save the field) is exactly the same as the use cases mentioned in those threads (do save the field, just don't ever output it).

This sort of thing is currently something I'd rather see provided for by third party packages - for exmple, since there are at least a couple of different write-only styles the user might want. The contributing docs give some guidelines on packaging third party apps, and I'd be very happy to see this packaged up and linked to from the main docs. Failing a proper PyPI package, you could also publish this on https://djangosnippets.org/ which we could also then link to from the main docs.

Thanks again for the contribution.

Member

tomchristie commented Dec 21, 2013

Hiya @kevmo314, thanks for the input.

Write-only fields have come up before, see https://groups.google.com/d/msg/django-rest-framework/UxWRbNE9NyU/s38xVmh6sw0J and https://groups.google.com/d/msg/django-rest-framework/abMsDCYbBRg/_T0zoHcDonIJ although I'm not sure if your behaviour (don't save the field) is exactly the same as the use cases mentioned in those threads (do save the field, just don't ever output it).

This sort of thing is currently something I'd rather see provided for by third party packages - for exmple, since there are at least a couple of different write-only styles the user might want. The contributing docs give some guidelines on packaging third party apps, and I'd be very happy to see this packaged up and linked to from the main docs. Failing a proper PyPI package, you could also publish this on https://djangosnippets.org/ which we could also then link to from the main docs.

Thanks again for the contribution.

@tomchristie

This comment has been minimized.

Show comment
Hide comment
@tomchristie

tomchristie Jan 14, 2014

Member

Change of heart - you were right on this. Pushed into 2.3.11

Member

tomchristie commented Jan 14, 2014

Change of heart - you were right on this. Pushed into 2.3.11

@sprymak

This comment has been minimized.

Show comment
Hide comment
@sprymak

sprymak Jan 16, 2014

Maybe it's a little bit too late but here is my 5 cents. I believe "write only" semantics assumes "those who can be assigned but can not be read". In this case I confused by "write_only" name. I think fields described here are rather "bogus" than "write only".

sprymak commented Jan 16, 2014

Maybe it's a little bit too late but here is my 5 cents. I believe "write only" semantics assumes "those who can be assigned but can not be read". In this case I confused by "write_only" name. I think fields described here are rather "bogus" than "write only".

@stanhu stanhu referenced this pull request in GetBlimp/django-rest-framework-jwt May 27, 2014

Closed

unexpected keyword argument 'write_only' #19

@COCOLMAN COCOLMAN referenced this pull request Mar 17, 2017

Closed

About "write_only_fields" being disabled #4995

5 of 6 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment