7👍
✅
This is complicated when you put all the function serializer to one, I would create a UserCreateSerializer
in this scenario:
class UserCreateSerializer(serializers.ModelSerializer):
"""A serializer for our user profile objects."""
class Meta:
model = models.User
extra_kwargs = {'password': {'write_only': True}}
fields = ['username', 'password', 'email', 'firstname', 'lastname', 'mobile'] # there what you want to initial.
def create(self, validated_data):
"""Create and return a new user."""
user = models.User(
email = validated_data['email'],
firstname = validated_data['firstname'],
lastname = validated_data['lastname'],
mobile = validated_data['mobile']
)
user.set_password(validated_data['password'])
user.save()
return user
Then you can use the UserCreateSerializer
in your UserCreateAPIView
.
18👍
The trick here is to include the ‘password’ field in the “fields” tuple so that password shows in BOTH ‘GET’ and ‘POST’, and then add ‘extra_kwargs’ to force ‘password’ field ONLY to appear in ‘POST’ form. Code as below:
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email',
'is_active', 'is_staff', 'is_superuser', 'password',)
# These fields are displayed but not editable and have to be a part of 'fields' tuple
read_only_fields = ('is_active', 'is_staff', 'is_superuser',)
# These fields are only editable (not displayed) and have to be a part of 'fields' tuple
extra_kwargs = {'password': {'write_only': True, 'min_length': 4}}
- Automatically round Django's DecimalField according to the max_digits and decimal_places attributes before calling save()
- Django cache framework. What is the difference between TIMEOUT and CACHE_MIDDLEWARE_SECONDS?
- Django datefield and timefield to python datetime
- Django – how to get user logged in (get_queryset in ListView)
5👍
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
def to_representation(self, obj):
rep = super(UserSerializer, self).to_representation(obj)
rep.pop('password', None)
return rep
- Django styling login forms and adding additional spans
- Shouldn't django's model get_or_create method be wrapped in a transaction?
- Call method once to set multiple fields in Django Rest Framework serializer
- Django success url using kwargs
- Django: Display a custom error message for admin validation error
1👍
To Make your password to not show password, you need to change style like following-
password = serializers.CharField(
style={'input_type': 'password'}
)
That’s it. I hope it helps.
👤Yash
- Start django shell with a temporary database
- After login the `rest-auth`, how to return more information?
- Django – Objects for business hours
- IntegrityError: null value in column "city_id " violates not-null constraint
1👍
Make Changes in Serializers.py file
password = serializers.CharField(
style={'input_type': 'password'},
min_length=6,
max_length=68,
write_only=True)
- Printing Receipt from Django Web Application
- How to enable history in Django shell in python
- Error when reverting an auto-generated migration for renaming a table in Django
- Django Class Based Generic Views URL Variable Passing
Source:stackexchange.com