Opened 10 years ago
Closed 5 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)
Change History (9)
by , 10 years ago
| Attachment: | set_password_user_migration.zip added |
|---|
comment:1 by , 10 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 10 years ago
| Resolution: | → invalid |
|---|---|
| Status: | assigned → closed |
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 , 10 years ago
| Cc: | added |
|---|
Was it a mistake to add use_in_migrations = True to the UserManager if it's not actually usable?
comment:4 by , 9 years ago
| Component: | Migrations → contrib.auth |
|---|---|
| Has patch: | set |
| Patch needs improvement: | set |
| Resolution: | invalid |
| Status: | closed → new |
| Triage Stage: | Unreviewed → Accepted |
Reopening per Markus's PR to solve this.
comment:5 by , 9 years ago
| Cc: | added |
|---|
comment:6 by , 6 years ago
| Patch needs improvement: | unset |
|---|
comment:8 by , 5 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Test project with failing migration dependent on contrib.auth