Equivalent of get_or_create for adding users

后端 未结 4 1827
北荒
北荒 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: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',
        )
    

提交回复
热议问题