Opened 9 years ago

Closed 4 years ago

#26445 closed Bug (fixed)

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: no
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 9 years ago.
Test project with failing migration dependent on contrib.auth

Download all attachments as: .zip

Change History (9)

by Markus Amalthea Magnuson, 9 years ago

Test project with failing migration dependent on contrib.auth

comment:1 by Markus Amalthea Magnuson, 9 years ago

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

comment:2 by Marten Kenbeek, 9 years ago

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 by Tim Graham, 9 years ago

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 by Tim Graham, 8 years ago

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 by Thomas Capricelli, 8 years ago

Cc: django2.20.orzelf@… added

comment:6 by Hasan Ramezani, 5 years ago

Patch needs improvement: unset
Version 0, edited 5 years ago by Hasan Ramezani (next)

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 7af8f41:

Refs #26445 -- Allowed using UserManager.create_user()/create_superuser() in migrations.

Used app config to lookup user model in _create_user().

Thanks Markus Holtermann for the review and initial patch.
Thanks Simon Charette for the implementation idea.

comment:8 by Mariusz Felisiak, 4 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top