For example I have the following serializer:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
'userid',
'password'
)
But I don't want to output password on GET (there are other fields in my real example of course). How can I do that without writing other serializer? Change the field list on the fly. Is there any way to do that?
Just one more thing to @Kevin Brown's solution.
Since
partial update
will also executeperform_update
, it would be better to add extra code as following.this should be what you need. I used a function view but you can use class View or ViewSet (override
get_serializer_class
) if you prefer.Note that
serializer_factory
also acceptexclude=
but, for security reason, I prefer usefields=
serializer_factory
create a Serializer class on the fly using an existing Serializer as base (same as djangomodelform_factory
)==============
You appear to be looking for a write-only field. So the field will be required on creation, but it won't be displayed back to the user at all (the opposite of a read-only field). Luckily, Django REST Framework now supports write-only fields with the
write_only
attribute.In Django REST Framework 3.0, you just need to add the extra argument to the
extra_kwargs
meta option.Because the
password
should be hashed (you are using Django's user, right?), you are going to need to also hash the password as it is coming in. This should be done on your view, most likely by overriding theperform_create
andperform_update
methods.In Django REST Framework 2.x, you need to completely redefine the
password
field on the serializer.In order to hash the password ahead of time in Django REST Framework 2.x, you need to override
pre_save
.This will solve the common issue with the other answers, which is that the same serializer that is used for creating/updating the user will also be used to return the updated user object as the response. This means that the password will still be returned in the response, even though you only wanted it to be write-only. The additional problem with this is that the password may or may not be hashed in the response, which is something you really don't want to do.
As far as i can tell from the docs, the fastest way would be to simply have 2 serializers becalled conditionally from your view.
Also, the docs show this other alternative, but it's a little too meta: http://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields
it involves creating smart initializer methods, gives an example. i'd just use 2 serializers, if i'd know those changes are the only ones i'll make. otherwise, check the example