[Django] #29019: Superuser creation for custom user model fails with required ManyToMany field

47 views
Skip to first unread message

Django

unread,
Jan 12, 2018, 5:06:54 PM1/12/18
to django-...@googlegroups.com
#29019: Superuser creation for custom user model fails with required ManyToMany
field
----------------------------------------+-----------------------------
Reporter: James Kirsop | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 2.0
Severity: Normal | Keywords: user custom
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------+-----------------------------
I've defined a custom user model with a ManyToMany field.

When running `manage.py createsuperuser` I receive the following error
after entering the user's email address:

{{{
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 371, in
execute_from_command_line
utility.execute()
File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-
packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-
packages/django/contrib/auth/management/commands/createsuperuser.py", line
59, in execute
return super().execute(*args, **options)
File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-
packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-
packages/django/contrib/auth/management/commands/createsuperuser.py", line
133, in handle
) if field.remote_field else '',
AttributeError: 'ManyToManyRel' object has no attribute 'field_name'
}}}

**Models**
My custom user model is defined as such (relevant pieces only included):
{{{
class OrgUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
orgs = models.ManyToManyField(Organisation)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['orgs']
objects = OrgUserManager()
}}}

and Organisations
{{{
class Organisation(models.Model):
name = models.CharField(max_length=60)

def __str__(self):
return self.name
}}}

It seems that if I remove the need for the Orgs to be a `REQUIRED_FIELD`
the issue goes away. However, it's central to my project and needs to be
defined on every user.

Happy to update the ticket with any other code snippets if required.

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

Django

unread,
Jan 12, 2018, 6:26:25 PM1/12/18
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------

Reporter: James Kirsop | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 2.0
Severity: Normal | Resolution:
Keywords: user custom | Triage Stage: Accepted

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

* stage: Unreviewed => Accepted


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

Django

unread,
Jan 30, 2018, 8:46:49 AM1/30/18
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------
Reporter: James Kirsop | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 2.0
Severity: Normal | Resolution:
Keywords: user custom | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+------------------------------------

Comment (by Sergey Yurchenko):

REQUIRED_FIELDS is field special for createsuperuser command.
[https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS]

In your forms or API this field would be still required if you remove it
from REQUIRED_FIELDS
I don`t think there is a good way to set m2m fields during creation if
superuser

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

Django

unread,
Jan 30, 2018, 10:06:50 AM1/30/18
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------
Reporter: James Kirsop | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 2.0
Severity: Normal | Resolution:
Keywords: user custom | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+------------------------------------

Comment (by Tim Graham):

Similar to foreign keys, ids could be accepted for ManyToManyFields. In
the case of a through model, a helpful error message could be displayed
indicating that it's not supported. In any case, Django shouldn't crash
without a helpful message.

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

Django

unread,
Feb 22, 2018, 5:25:04 PM2/22/18
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+-------------------------------------------
Reporter: James Kirsop | Owner: Williams Mendez
Type: Bug | Status: assigned
Component: contrib.auth | Version: 2.0
Severity: Normal | Resolution:
Keywords: user custom | Triage Stage: Accepted

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

* owner: nobody => Williams Mendez
* status: new => assigned


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

Django

unread,
May 26, 2018, 10:55:24 AM5/26/18
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+-------------------------------------------
Reporter: James Kirsop | Owner: Williams Mendez
Type: Bug | Status: assigned
Component: contrib.auth | Version: 2.0
Severity: Normal | Resolution:
Keywords: user custom | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+-------------------------------------------

Comment (by Lenno Nagel):

Hi!

We explored a couple of ideas regarding this during DjangoCon Europe 2018.

The aspect that blocks implementation is the create_superuser function in
the default UserManager class that has no support for M2M field data
present in **kwargs. Such data would have to be extracted from kwargs and
handled after the user.save(..) call, however we did not feel comfortable
with proposing code for this solution without prior discussion.

It was found that it is perhaps best simply to warn the user whenever M2M
fields have been added to REQUIRED_FIELDS and suggest that in this case, a
custom user manager must also be written that handles the data properly.
We could not find a safe way to detect whether the default manager has
been overridden or whether it would handle the M2M field in the
create_superuser function, so this warning has to be marked as ignored
manually by the user.

A proposed solution:
https://github.com/django/django/pull/9990

Regards,
Lenno

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

Django

unread,
Aug 1, 2019, 3:38:19 AM8/1/19
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------------
Reporter: James Kirsop | Owner: Hasan Ramezani
Type: Bug | Status: assigned
Component: contrib.auth | Version: 2.0
Severity: Normal | Resolution:
Keywords: user custom | 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):

* owner: Williams Mendez => Hasan Ramezani
* has_patch: 0 => 1


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

Django

unread,
Aug 5, 2019, 4:19:29 AM8/5/19
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------------
Reporter: James Kirsop | Owner: Hasan Ramezani
Type: Bug | Status: assigned
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: user custom | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1
* version: 2.0 => master


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

Django

unread,
Aug 7, 2019, 8:36:12 AM8/7/19
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------------
Reporter: James Kirsop | Owner: Hasan Ramezani
Type: Bug | Status: assigned
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: user custom | 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


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

Django

unread,
Aug 26, 2019, 9:17:41 AM8/26/19
to django-...@googlegroups.com
#29019: createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
------------------------------+------------------------------------------
Reporter: James Kirsop | Owner: Hasan Ramezani
Type: Bug | Status: closed
Component: contrib.auth | Version: master
Severity: Normal | Resolution: fixed
Keywords: user custom | Triage Stage: Accepted
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:"03dbdfd9bbbbd0b0172aad648c6bbe3f39541137" 03dbdfd9]:
{{{
#!CommitTicketReference repository=""
revision="03dbdfd9bbbbd0b0172aad648c6bbe3f39541137"
Fixed #29019 -- Added ManyToManyField support to REQUIRED_FIELDS.
}}}

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

Reply all
Reply to author
Forward
0 new messages