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.
* Attachment "django.patch" added.
Proposed patch to fix the bug
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>
* Attachment "33151.diff" added.
Proposed patch to fix the bug
--
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:2>
* owner: nobody => Christophe Henry
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:3>
* cc: Hasan Ramezani (added)
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:4>
* needs_better_patch: 1 => 0
* has_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:5>
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>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:7>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:8>
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:9>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33151#comment:10>
* 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>
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>