0

Here's the model:

class CustomerStatus(models.TextChoices):
    ACTIVE = 'ACT', 'Active'
    EXPIRED = 'EXP', 'Expired'
    REVOKED = 'REV', 'Revoked'


class Customer(models.Model):
    email = models.EmailField(max_length=254, unique=True)
    password = models.CharField(max_length=128)
    created = models.DateTimeField(auto_now_add=True)
    status = models.CharField(
        max_length=3, choices=CustomerStatus.choices, default=CustomerStatus.ACTIVE
    )

and the serializer:

class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = ['email', 'password', 'created', 'status']
        extra_kwargs = {'password': {'write_only': True}}

But I want to hash the password and that's not working. I check the field in Admin site and it's stored in plain text.

I've tried these:

def create(self, validated_data):
        validated_data['password'] = make_password(validated_data['password'])
        return super(CustomerSerializer, self).create(validated_data)

and this one:

def create(self, validated_data):
        validated_data['password'] = make_password(validated_data['password'])
        return super().create(validated_data)

and this one:

customer = Customer.objects.create(
            email=validated_data['email'],
            password=make_password(validated_data['password']),
        )

        return customer

I'm running out of options.

EDIT 1

If I override save method and hash the password everything is ok. So I should not expect this to work in create method of the serializer. Correct?

Omid Shojaee
  • 333
  • 1
  • 4
  • 20
  • 1
    Does this answer your question? [How to Hash Django user password in Django Rest Framework?](https://stackoverflow.com/questions/41332528/how-to-hash-django-user-password-in-django-rest-framework) – Sunderam Dubey Aug 23 '22 at 10:45
  • @SunderamDubey No. Tried all answers already. That question is 5 years old. Something has changed since. – Omid Shojaee Aug 23 '22 at 10:49
  • Nothing has changed, you aren't using the `User` model, or `AbstractUser` models, are you? That might be the issue. I am not sure. – nigel239 Aug 23 '22 at 11:02
  • @nigel239 No I'm not subclassing ```AbstractUser``` and I'm not using the ```User``` model. I have my own model. And ```make_password``` works in ```save``` method but not with the ```create``` method. – Omid Shojaee Aug 23 '22 at 11:07
  • https://stackoverflow.com/a/46908205/18020941 This is the same question @SunderamDubey provided, and should solve your issue. This method goes into the serializer in the view. Just make sure to say `instance = serializer.save(commit=False)`, instead of `instance = serializer.save()` – nigel239 Aug 23 '22 at 11:09
  • 1
    @nigel239 Thanks. I've seen that answer. All I want to know is why this doesn't work in ```create``` method. – Omid Shojaee Aug 23 '22 at 11:30
  • @OmidShojaee Can you share the code present in class `UserManager(BaseUserManager)`? – Gonçalo Peres Sep 28 '22 at 13:27

0 Answers0