[Fixed]-Using User.objects.get_or_create() gives invalid password format in django?

31👍

You need to use the User.set_password method to set a raw password.

E.g.,

from django.contrib.auth.models import User
user, created = User.objects.get_or_create(username="testuser2")
user.set_password('123')
user.save()

6👍

Almost correct except we don’t want to set password of existing users

from django.contrib.auth.models import User
user, created = User.objects.get_or_create(username="testuser2")
if created:
          # user was created
          # set the password here
          user.set_password('123')
          user.save()
       else:
          # user was retrieved
👤Yash

1👍

As mentioned in the documentation.

The most direct way to create users is to use the included create_user() helper function.

from django.contrib.auth.models import User
user = User.objects.create_user(username="testuser2",password="123")

Leave a comment