Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a django User using DRF's ModelSerializer

In django, creating a User has a different and unique flow from the usual Model instance creation. You need to call create_user() which is a method of BaseUserManager.

Since django REST framework's flow is to do restore_object() and then save_object(), it's not possible to simply create Users using a ModelSerializer in a generic create API endpoint, without hacking you way through.

What would be a clean way to solve this? or at least get it working using django's built-in piping?

Edit:

Important to note that what's specifically not working is that once you try to authenticate the created user instance using django.contrib.auth.authenticate it fails if the instance was simply created using User.objects.create() and not .create_user().

like image 758
ydaniv Avatar asked Nov 04 '13 13:11

ydaniv


2 Answers

Eventually I've overridden the serializer's restore_object method and made sure that the password being sent is then processes using instance.set_password(password), like so:

def restore_object(self, attrs, instance=None):
    if not instance:
        instance = super(RegisterationSerializer, self).restore_object(attrs, instance)
    instance.set_password(attrs.get('password'))
    return instance

Thanks everyone for help!

like image 87
ydaniv Avatar answered Oct 16 '22 07:10

ydaniv


Another way to fix this is to overwrite pre_save(self, obj) method in your extension of viewsets.GenericViewSet like so:

def pre_save(self, obj):
    """ We have to encode the password in the user object that will be
    saved before saving it.
    """
    viewsets.GenericViewSet.pre_save(self, obj)
    # Password is raw right now, so set it properly (encoded password will
    # overwrite the raw one then).
    obj.user.set_password(obj.user.password)

Edit:

Note that the obj in the code above contains the instance of User class. If you use Django's user model class directly, replace obj.user with obj in the code (the last line in 2 places).

like image 38
Kristijan Mitrovic Avatar answered Oct 16 '22 07:10

Kristijan Mitrovic