[Django] #33151: Super user creation always raises an error in non-interactive mode when on required field is a ForeignKey

3 views
Skip to first unread message

Django

unread,
Sep 28, 2021, 8:21:31 AM9/28/21
to django-...@googlegroups.com
#33151: Super user creation always raises an error in non-interactive mode when on
required field is a ForeignKey
--------------------------------------------+------------------------
Reporter: Christophe Henry | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
#21755 added `ForeignKey` support to `REQUIRED_FIELDS` in
`createsuperuser` command but this support is not working in non-
interactive mode.

The buggy line is
[https://github.com/django/django/blob/main/django/contrib/auth/management/commands/createsuperuser.py#L187
this line]. If `value` is an integer, `field.clean()` simply returns it
after validation while
[https://github.com/django/django/blob/main/django/contrib/auth/management/commands/createsuperuser.py#L189
`create_superuser(**user_data)`] on the next line would expect a model
instance for the `ForeignKey` field.

If you go one step further and override `createsuperuser` to pass an
instance of the model, then `field.clean()` raises an error because
[https://github.com/django/django/blob/main/django/db/models/fields/__init__.py#L670
`ForeignKey.to_python()`] expects an integer.

There may be the same problem with `ManyToManyField`.

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

Django

unread,
Sep 28, 2021, 8:24:42 AM9/28/21
to django-...@googlegroups.com
#33151: Super user creation always raises an error in non-interactive mode when on
required field is a ForeignKey
----------------------------------+--------------------------------------

Reporter: Christophe Henry | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 3.1
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 Christophe Henry):

* Attachment "django.patch" added.

Proposed patch to fix the bug

Django

unread,
Sep 28, 2021, 8:26:30 AM9/28/21
to django-...@googlegroups.com
#33151: Super user creation always raises an error in non-interactive mode when on
required field is a ForeignKey
----------------------------------+--------------------------------------

Reporter: Christophe Henry | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 3.1
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
----------------------------------+--------------------------------------

Comment (by Christophe Henry):

I just posted a patch proposing a fix for this bug. I'm open to comments
on this solutions and suggestions on how to fix the `ManyToManyField`
case.

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

Django

unread,
Sep 28, 2021, 8:28:49 AM9/28/21
to django-...@googlegroups.com
#33151: Super user creation always raises an error in non-interactive mode when on
required field is a ForeignKey
----------------------------------+--------------------------------------

Reporter: Christophe Henry | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 3.1
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 Christophe Henry):

* Attachment "33151.diff" added.

Proposed patch to fix the bug

--

Django

unread,
Sep 28, 2021, 8:29:36 AM9/28/21
to django-...@googlegroups.com
#33151: Super user creation always raises an error in non-interactive mode when on
required field is a ForeignKey
----------------------------------+--------------------------------------

Reporter: Christophe Henry | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1


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

Django

unread,
Sep 28, 2021, 8:30:47 AM9/28/21
to django-...@googlegroups.com
#33151: Super user creation always raises an error in non-interactive mode when on
required field is a ForeignKey
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

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

* owner: nobody => Christophe Henry
* status: new => assigned


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

Django

unread,
Sep 29, 2021, 7:41:11 AM9/29/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a
ManyToManyField/ForeignKey is in REQUIRED_FIELDS.

-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

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

* cc: Hasan Ramezani (added)
* stage: Unreviewed => Accepted


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

Django

unread,
Oct 4, 2021, 8:50:39 AM10/4/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a
ManyToManyField/ForeignKey is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Christophe Henry):

* needs_better_patch: 1 => 0
* has_patch: 1 => 0
* needs_tests: 1 => 0


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

Django

unread,
Oct 4, 2021, 8:52:22 AM10/4/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a
ManyToManyField/ForeignKey is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Christophe Henry):

[https://github.com/django/django/pull/14913 #14913] is fixing this one
and is ready for review.

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

Django

unread,
Oct 5, 2021, 1:59:01 PM10/5/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a
ManyToManyField/ForeignKey is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1

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 Jacob Walls):

* has_patch: 0 => 1


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

Django

unread,
Oct 7, 2021, 2:39:34 AM10/7/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a
ManyToManyField/ForeignKey is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1

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 Mariusz Felisiak):

* needs_better_patch: 0 => 1


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

Django

unread,
Oct 7, 2021, 2:40:10 AM10/7/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a ManyToManyField
is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1

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
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:9>

Django

unread,
Oct 7, 2021, 6:40:09 AM10/7/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a ManyToManyField
is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: assigned
Component: contrib.auth | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:10>

Django

unread,
Oct 7, 2021, 7:11:00 AM10/7/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a ManyToManyField
is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: closed
Component: contrib.auth | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"df2d2bc95c451c6366fd522a5a1e6ed84f459f31" df2d2bc9]:
{{{
#!CommitTicketReference repository=""
revision="df2d2bc95c451c6366fd522a5a1e6ed84f459f31"
Fixed #33151 -- Fixed createsuperuser crash for many-to-many required
fields in non-interactive mode.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:11>

Django

unread,
Oct 7, 2021, 7:11:45 AM10/7/21
to django-...@googlegroups.com
#33151: createsuperuser doesn't work in non-interactive mode if a ManyToManyField
is in REQUIRED_FIELDS.
-------------------------------------+-------------------------------------
Reporter: Christophe Henry | Owner:
| Christophe Henry
Type: Bug | Status: closed
Component: contrib.auth | Version: 3.1

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
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:"f2a59630f48f0daac46be62e234942dcdc1f366a" f2a5963]:
{{{
#!CommitTicketReference repository=""
revision="f2a59630f48f0daac46be62e234942dcdc1f366a"
[4.0.x] Fixed #33151 -- Fixed createsuperuser crash for many-to-many


required fields in non-interactive mode.

Backport of df2d2bc95c451c6366fd522a5a1e6ed84f459f31 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:12>

Reply all
Reply to author
Forward
0 new messages