{"record":{"id":"64e1269034df6283","repo":"django/django","slug":"required-field-s-specifies-a-many-to-many-relat","errorCode":null,"errorMessage":"Required field '%s' specifies a many-to-many relation through model, which is not supported.","messagePattern":"Required field '(.+?)' specifies a many-to-many relation through model, which is not supported\\.","errorType":"console","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"django/contrib/auth/management/commands/createsuperuser.py","lineNumber":69,"sourceCode":"                \"any other required field. Superusers created with --noinput will \"\n                \"not be able to log in until they're given a valid password.\"\n                % self.UserModel.USERNAME_FIELD\n            ),\n        )\n        parser.add_argument(\n            \"--database\",\n            default=DEFAULT_DB_ALIAS,\n            choices=tuple(connections),\n            help='Specifies the database to use. Default is \"default\".',\n        )\n        for field_name in self.UserModel.REQUIRED_FIELDS:\n            field = self.UserModel._meta.get_field(field_name)\n            if field.many_to_many:\n                if (\n                    field.remote_field.through\n                    and not field.remote_field.through._meta.auto_created\n                ):\n                    raise CommandError(\n                        \"Required field '%s' specifies a many-to-many \"\n                        \"relation through model, which is not supported.\" % field_name\n                    )\n                else:\n                    parser.add_argument(\n                        \"--%s\" % field_name,\n                        action=\"append\",\n                        help=(\n                            \"Specifies the %s for the superuser. Can be used \"\n                            \"multiple times.\" % field_name,\n                        ),\n                    )\n            else:\n                parser.add_argument(\n                    \"--%s\" % field_name,\n                    help=\"Specifies the %s for the superuser.\" % field_name,\n                )\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/auth/management/commands/createsuperuser.py#L51-L87","documentation":"Raised during createsuperuser.add_arguments() when a field listed in the custom user model's REQUIRED_FIELDS is a many-to-many field whose through model was NOT auto-created by Django. createsuperuser cannot drive membership in an explicit through table from CLI flags, so it refuses to build the argument parser.","triggerScenarios":"Defining a custom user model (AUTH_USER_MODEL) with a REQUIRED_FIELDS entry that is a ManyToManyField(using an explicit `through=Membership` model), then running `manage.py createsuperuser`.","commonSituations":"Adding a roles/groups-style M2M with a custom through model to a custom user and accidentally listing it in REQUIRED_FIELDS; refactoring a user model and forgetting that REQUIRED_FIELDS should only contain simple required columns.","solutions":["Remove the M2M-with-through field from your user model's REQUIRED_FIELDS (REQUIRED_FIELDS should only name fields needed to create a minimal superuser).","If the relation must be populated, assign it after creation via the shell/admin rather than via createsuperuser flags.","Alternatively, use an auto-created M2M (no explicit through) if you truly need it as a CLI flag."],"exampleFix":"// before:\nclass CustomUser(AbstractBaseUser):\n    groups = models.ManyToManyField(Group, through=Membership)\n    REQUIRED_FIELDS = ['groups']  # raises on createsuperuser\n\n// after:\nclass CustomUser(AbstractBaseUser):\n    groups = models.ManyToManyField(Group, through=Membership)\n    REQUIRED_FIELDS = ['email']  # only plain required fields","handlingStrategy":"validation","validationCode":"from django.contrib.auth import get_user_model\nUserModel = get_user_model()\nfor name in UserModel.REQUIRED_FIELDS:\n    f = UserModel._meta.get_field(name)\n    if f.many_to_many and f.remote_field.through and not f.remote_field.through._meta.auto_created:\n        raise SystemExit(\n            f'REQUIRED_FIELDS contains M2M-with-through {name!r}; remove it before running createsuperuser'\n        )","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep REQUIRED_FIELDS limited to simple, non-null, non-M2M columns needed to create a superuser.","Never list a M2M with a custom through model in REQUIRED_FIELDS.","Populate complex relations after creation via shell/admin, not via createsuperuser flags."],"tags":["django","createsuperuser","custom-user-model","many-to-many","required-fields"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}