﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21755	Add ForeignKey support to REQUIRED_FIELDS	Chris Jerdonek	nobody	"If `instance=None` is passed to `_route_db` in db.utils, then an exception like the following is raised:

{{{
  ...
  File "".../django/core/management/base.py"", line 285, in execute
    output = self.handle(*args, **options)
  File "".../django/contrib/auth/management/commands/createsuperuser.py"", line 96, in handle
    username = self.username_field.clean(raw_value, None)
  File "".../django/db/models/fields/__init__.py"", line 255, in clean
    self.validate(value, model_instance)
  File "".../django/db/models/fields/related.py"", line 1199, in validate
    using = router.db_for_read(model_instance.__class__, instance=model_instance)
  File "".../django/db/utils.py"", line 250, in _route_db
    return hints['instance']._state.db or DEFAULT_DB_ALIAS
AttributeError: 'NoneType' object has no attribute '_state'
}}}

The code is [https://github.com/django/django/blob/856aaaf2b15228bd5babbe44654c3c1ca39e8fd7/django/db/utils.py#L243 here]:

{{{
try:
    return hints['instance']._state.db or DEFAULT_DB_ALIAS
except KeyError:
    return DEFAULT_DB_ALIAS
}}}

It would be better if the None case were handled in the same way that KeyError is being caught.

"	New feature	closed	contrib.auth	dev	Normal	fixed		chris.jerdonek@… Tim Graham anubhav9042@…	Accepted	1	0	0	0	0	0
