Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How remove password from response in django

Code:
SERILIZERS

class RegisterSerializer(serializers.ModelSerializer):
class Meta:
    model = User
    fields = ("email", "user_name", "password")

def create(self, validated_data):
    return User.objects.create_user(**validated_data)

VIEWS

@api_view(["POST"])
@permission_classes([AllowAny])
def CreateUserView(request):
    serializer = RegisterSerializer(data=request.data)
    if serializer.is_valid():
       user = serializer.save()
       if user:
          return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

MODELS

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class CustomAccountManager(BaseUserManager):
def create_user(self, email, user_name, password, **other_fields):

    if not email:
        raise ValueError(_("You must provide an email address"))
    email = self.normalize_email(email)
    user = self.model(email=email, user_name=user_name, **other_fields)
    user.set_password(password)
    user.save()
    return user

After i post user i got response data with password.

{
"email": "mawqgvxdfdat1@mat.com",
"user_name": "sdwxcvgfsdefsdf",
"password": "pbkdf2_sha256$320000$g74DLAMRDypcK6LuGriBio$
}

How can i remove that password from returned data?

>Solution :

Into serializer

fields = ("email", "user_name",)
extra_kwargs = {'password': {'write_only': True}}

Actually delete password field from it

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading