Opened 20 months ago

Last modified 2 months ago

#26445 new Bug

Can't set password on User during migration that depends on contrib.auth

Reported by: Markus Amalthea Magnuson Owned by: Markus Amalthea Magnuson
Component: contrib.auth Version: 1.9
Severity: Normal Keywords:
Cc: Markus Holtermann, django2.20.orzelf@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I have an app with migrations that depend on the contrib.auth migrations. In one of my own migrations I want to create User objects and set their password. When trying to set a password for User object either through .set_password() or directly on creation through e.g. .create_superuser() I get this error:

AttributeError: 'User' object has no attribute 'set_password'

Attached is a test project. Create a virtual env with Django 1.9 and run ./manage.py migrate in it to see the error.

It seems like the password related methods are not available on the User object, only in the default manager, which I gather is not available in migrations. Is this intentional?

Attachments (1)

set_password_user_migration.zip (11.7 KB) - added by Markus Amalthea Magnuson 20 months ago.
Test project with failing migration dependent on contrib.auth

Download all attachments as: .zip

Change History (6)

Changed 20 months ago by Markus Amalthea Magnuson

Test project with failing migration dependent on contrib.auth

comment:1 Changed 20 months ago by Markus Amalthea Magnuson

Owner: changed from nobody to Markus Amalthea Magnuson
Status: newassigned

comment:2 Changed 20 months ago by Marten Kenbeek

Resolution: invalid
Status: assignedclosed

It is expected behaviour that these methods are not available in your migrations. The reason is shortly explained in Historical models: arbitrary code can't be serialized, so there is no general way to make the historical methods available in your migrations.

To avoid this problem you can use the make_password function directly and set the hashed password on your user:

from django.contrib.auth.hashers import make_password

user.password = make_password('password')

comment:3 Changed 20 months ago by Tim Graham

Cc: Markus Holtermann added

Was it a mistake to add use_in_migrations = True to the UserManager if it's not actually usable?

comment:4 Changed 17 months ago by Tim Graham

Component: Migrationscontrib.auth
Has patch: set
Patch needs improvement: set
Resolution: invalid
Status: closednew
Triage Stage: UnreviewedAccepted

Reopening per Markus's PR to solve this.

comment:5 Changed 16 months ago by Thomas Capricelli

Cc: django2.20.orzelf@… added
Note: See TracTickets for help on using tickets.
Back to Top