Opened 6 days ago

Last modified 6 days ago

#29019 new Bug

createsuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS

Reported by: James Kirsop Owned by: nobody
Component: contrib.auth Version: 2.0
Severity: Normal Keywords: user custom
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I've defined a custom user model with a ManyToMany field.

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

Traceback (most recent call last):
  File "./", line 22, in <module>
  File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-packages/django/core/management/", line 371, in execute_from_command_line
  File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-packages/django/core/management/", line 365, in execute
  File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-packages/django/core/management/", 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/", line 59, in execute
    return super().execute(*args, **options)
  File "/Users/jkirsop/Development/artemis/venv/lib/python3.6/site-packages/django/core/management/", 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/", line 133, in handle
    ) if field.remote_field else '',
AttributeError: 'ManyToManyRel' object has no attribute 'field_name'

My custom user model is defined as such (relevant pieces only included):

class OrgUser(AbstractBaseUser, PermissionsMixin):
	email = models.EmailField(
		verbose_name='email address',
	orgs = models.ManyToManyField(Organisation)
        objects = OrgUserManager()

and Organisations

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

	def __str__(self):

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.

Change History (1)

comment:1 Changed 6 days ago by Tim Graham

Summary: Superuser creation for custom user model fails with required ManyToMany fieldcreatesuperuser crashes if a ManyToManyField is in REQUIRED_FIELDS
Triage Stage: UnreviewedAccepted
Note: See TracTickets for help on using tickets.
Back to Top