开发者

Equivalent of get_or_create for adding users

开发者 https://www.devze.com 2023-04-06 10:40 出处:网络
Is there a simpler way to add a user than with the following pattern? try: new_user = User.objects.c开发者_StackOverflowreate_user(username, email, password)

Is there a simpler way to add a user than with the following pattern?

    try:
        new_user = User.objects.c开发者_StackOverflowreate_user(username, email, password)
    except IntegrityError:
        messages.info(request, "This user already exists.")
    else:
        new_user.first_name = first_name
        # continue with other things


In Django 1.4, get_or_create() exists for User.

from django.contrib.auth.models import User

_user = User.objects.get_or_create(
        username=u'bob',
        password=u'bobspassword',
    )


It is better not to catch IntegrityError as it can happen for other reasons. You need to check if user exists, excluding the password. If user already exists, set the password.

user, created = User.objects.get_or_create(username=username, email=email)
if created:
    user.set_password(password)
    user.save()


I don't think so. But you can proxify the Django User:

class MyUser(User):
    class Meta:
        proxy = True

    def get_or_create(self, username, email, password, *args, **kwargs):
        try:
            new_user = User.objects.create_user(username, email, password)
        except IntegrityError:
            return User.objects.get(username=username, email=email)
        else:
            new_user.first_name = kwargs['first_name'] # or what you want
            ...etc...

        return new_user


This method should solve this problem but keep in mind that in the database, password is kept as a hash of the password, and as pointed before, "get_or_create" makes an exact lookup. So before the lookup actually happens, we "pop" the password from the kwargs. This method on your custom UserManager:

def get_or_create(self, defaults=None, **kwargs):
    password = kwargs.pop('password', None)
    obj, created = super(UserManager, self).get_or_create(defaults, **kwargs)
    if created and password:
        obj.set_password(password)
        obj.save()
    return obj, created
0

精彩评论

暂无评论...
验证码 换一张
取 消