#21755: db.utils _route_db can raise AttributeError: 'NoneType' object has no attribute '_state' -------------------------------------+------------------------------------- Reporter: cjerdonek | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 1.6 (models, ORM) | Resolution: Severity: Normal | Triage Stage: Keywords: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 1 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by cjerdonek): Hi, I've been meaning to elaborate on this because I'm not sure if this should be changed as I've described. This error occurred when setting the `USERNAME_FIELD` to a foreign key field and running the `createsuperuser` command. The base field class's `validate()` method [https://github.com/django/django/blob/9ddc358e8d598f38d8443be708962d8b1f34b7e2/django/db/models/fields/__init__.py#L442 doesn't use] the `model_instance` argument, but the `ForeignKey` field version's [https://github.com/django/django/blob/9ddc358e8d598f38d8443be708962d8b1f34b7e2/django/db/models/fields/related.py#L1616 does]. Obviously, I needed to change the `createsuperuser` command in other ways to get `createsuperuser` to work in my use case above, but this error I encountered in the process seemed unnecessarily obscure. The possible resolutions of this issue I can think of are: * pass a valid `model_instance` instead of `None` in the default implementation of `createsuperuser`, even though it is not needed in the out-of-the-box case, * allow `None` to be passed in the foreign key field version of `validate()` (for example by catching the AttributeError in `db.utils _route_db()` as I described in my original suggestion), * raising a simpler-to-decipher error earlier in the process (e.g. by raising in `createsuperuser` or elsewhere that foreign key fields aren't allowed), or * leaving things as is. Thanks for considering this. -- Ticket URL: <https://code.djangoproject.com/ticket/21755#comment:3> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.29b29a9d447861a75733a5cc9ca0899b%40djangoproject.com. For more options, visit https://groups.google.com/groups/opt_out.