[Django] #26445: Can't set password on User during migration that depends on contrib.auth

22 views
Skip to first unread message

Django

unread,
Apr 2, 2016, 11:25:05 AM4/2/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
---------------------------------+--------------------
Reporter: giuliettamasina | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
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?

--
Ticket URL: <https://code.djangoproject.com/ticket/26445>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 2, 2016, 11:25:33 AM4/2/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-----------------------------+----------------------------

Reporter: giuliettamasina | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------+----------------------------
Changes (by giuliettamasina):

* Attachment "set_password_user_migration.zip" added.

Test project with failing migration dependent on contrib.auth

Django

unread,
Apr 2, 2016, 11:26:03 AM4/2/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: giuliettamasina | Owner:
| giuliettamasina
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by giuliettamasina):

* owner: nobody => giuliettamasina
* needs_better_patch: => 0
* status: new => assigned
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:1>

Django

unread,
Apr 2, 2016, 11:38:42 AM4/2/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: giuliettamasina | Owner:
| giuliettamasina
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by knbk):

* status: assigned => closed
* resolution: => invalid


Comment:

It is expected behaviour that these methods are not available in your
migrations. The reason is shortly explained in
[https://docs.djangoproject.com/en/1.9/topics/migrations/#historical-
models 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')
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:2>

Django

unread,
Apr 2, 2016, 11:50:53 AM4/2/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: giuliettamasina | Owner:
| giuliettamasina
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* cc: MarkusH (added)


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:3>

Django

unread,
Jun 28, 2016, 3:10:47 PM6/28/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: giuliettamasina | Owner:
| giuliettamasina
Type: Bug | Status: new
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: closed => new
* needs_better_patch: 0 => 1
* component: Migrations => contrib.auth
* has_patch: 0 => 1
* resolution: invalid =>
* stage: Unreviewed => Accepted


Comment:

Reopening per [https://github.com/django/django/pull/6578 Markus's PR] to
solve this.

--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:4>

Django

unread,
Aug 2, 2016, 7:07:54 PM8/2/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: giuliettamasina | Owner:
| giuliettamasina
Type: Bug | Status: new

Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by orzel):

* cc: django2.20.orzelf@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:5>

Django

unread,
Sep 17, 2016, 2:57:14 PM9/17/16
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: giuliettamasina | Owner:
| giuliettamasina
Type: Bug | Status: new

Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: 2.0 | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* keywords: => 2.0


--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:6>

Django

unread,
Mar 10, 2020, 5:50:17 PM3/10/20
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: Markus Amalthea | Owner: Markus
Magnuson | Amalthea Magnuson
Type: Bug | Status: new

Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* needs_better_patch: 1 => 0

Django

unread,
Jul 6, 2020, 6:12:16 AM7/6/20
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: Markus Amalthea | Owner: Markus
Magnuson | Amalthea Magnuson
Type: Bug | Status: new

Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"7af8f4127397279d19ef7c7899e93018274e2f9b" 7af8f41]:
{{{
#!CommitTicketReference repository=""
revision="7af8f4127397279d19ef7c7899e93018274e2f9b"
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.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:7>

Django

unread,
Jul 6, 2020, 6:13:27 AM7/6/20
to django-...@googlegroups.com
#26445: Can't set password on User during migration that depends on contrib.auth
-------------------------------------+-------------------------------------
Reporter: Markus Amalthea | Owner: Markus
Magnuson | Amalthea Magnuson
Type: Bug | Status: closed
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/26445#comment:8>

Reply all
Reply to author
Forward
0 new messages