Write only fields

1,734 views
Skip to first unread message

mars...@poundpay.com

unread,
Mar 27, 2013, 1:59:21 PM3/27/13
to django-res...@googlegroups.com
I did some googling but didn't find any results.

Is it possible to have write only fields? E.g. I want the serializer to validate some data in the payload but I don't want to echo that back.

E.g. a password field shouldn't be echoed back:


class UserLoginsSerializer(serializers.Serializer):

    email_address = serializers.EmailField(required=True)

    password = serializers.CharField(required=True, write_only=True)

Matthieu Neamar

unread,
Mar 27, 2013, 2:04:21 PM3/27/13
to django-res...@googlegroups.com



--
You received this message because you are subscribed to the Google Groups "django-rest-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mars...@poundpay.com

unread,
Mar 27, 2013, 2:45:07 PM3/27/13
to django-res...@googlegroups.com
Yep, that's not quite as simple but it will work. Thanks!


On Wednesday, March 27, 2013 11:04:21 AM UTC-7, Neamar Tucote wrote:
On Wed, Mar 27, 2013 at 6:59 PM, <mars...@poundpay.com> wrote:
I did some googling but didn't find any results.

Is it possible to have write only fields? E.g. I want the serializer to validate some data in the payload but I don't want to echo that back.

E.g. a password field shouldn't be echoed back:


class UserLoginsSerializer(serializers.Serializer):

    email_address = serializers.EmailField(required=True)

    password = serializers.CharField(required=True, write_only=True)

--
You received this message because you are subscribed to the Google Groups "django-rest-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Pedro H S Teixeira

unread,
Jun 13, 2013, 12:28:19 PM6/13/13
to django-res...@googlegroups.com
Another option is to override "def data" to delete it from the serialized data:


class MySerializer(serializers.HyperlinkedModelSerializer):
   write_only_field = erializers.WritableField(source='my_field_in_model', required=False)

    @property
    def data(self):
        d = super(MySerializer, self).data
        del d['write_only_field']
        return d

Robert Kajic

unread,
Sep 29, 2013, 6:35:11 AM9/29/13
to django-res...@googlegroups.com
I had the same problem. Here is how I solved it:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    created_at = serializers.Field(source='date_joined')

    class Meta:
        model = User
        fields = ['id', 'url', 'username', 'password', 'first_name', 'last_name', 'email', 'created_at', ]

    def restore_object(self, attrs, instance=None):
        user = super(UserSerializer, self).restore_object(attrs, instance)
        user.set_password(attrs['password'])
        return user

    def to_native(self, obj):
        ret = super(UserSerializer, self).to_native(obj)
        del ret['password']
        return ret
Reply all
Reply to author
Forward
0 new messages