Equivalent of get_or_create for adding users

后端 未结 4 1825
北荒
北荒 2021-02-07 02:05

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

    try:
        new_user = User.objects.create_user(username, email, password)
    except          


        
相关标签:
4条回答
  • 2021-02-07 02:43

    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
    
    0 讨论(0)
  • 2021-02-07 02:49

    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',
        )
    
    0 讨论(0)
  • 2021-02-07 02:50

    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 讨论(0)
  • 2021-02-07 03:01

    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 not created:
        user.set_password(password)
    
    0 讨论(0)
提交回复
热议问题