Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign key as required field on Django 1.5 configurable user model, createsuperuser says: AttributeError: 'NoneType' object has no attribute '_state'
    primarykey
    data
    text
    <p>Using the new <a href="https://docs.djangoproject.com/en/dev/releases/1.5/#configurable-user-model" rel="noreferrer">Configurable user model</a> from Django 1.5 (1, 5, 0, 'beta', 2) I get this error while running <a href="https://docs.djangoproject.com/en/dev/ref/django-admin/#createsuperuser" rel="noreferrer">manage.py createsuperuser</a> trying to set a foreign key of a required field:</p> <blockquote> <p>AttributeError: 'NoneType' object has no attribute '_state'</p> </blockquote> <p>Since I've a <a href="https://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures" rel="noreferrer">fixtures/initial_data.yaml</a> with values I need for my user model, workflow is:</p> <ol> <li>Create database</li> <li>python manage.py syncdb</li> <li>Answer "no" to the question <em>You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no)</em></li> <li><em>Installed 14264 object(s) from 1 fixture(s)</em></li> <li>python manage.py createsuperuser</li> </ol> <p>I try to create superuser after fixtures are imported, so this is not a problem caused by an empty database table for the City model.</p> <p>Code excerpt, based on <a href="https://docs.djangoproject.com/en/dev/topics/auth/#a-full-example" rel="noreferrer">documentation</a>:</p> <p><strong>models.py</strong></p> <pre><code>class City(models.Model): city = models.CharField(max_length=70, help_text="City.") state = models.CharField(max_length=2, help_text="State.") class Meta: ordering = ['city'] def __unicode__(self): return "%s (%s)" % (self.city, self.state) class PersonaManager(BaseUserManager): [...] def create_superuser(self, email, name, birthplace, password): """ Creates and saves a superuser with the given email, date of birth and password. """ user = self.create_user(email, password=password, name=name, birthplace=birthplace, ) user.is_admin = True user.save(using=self._db) return user class Person(AbstractBaseUser): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, db_index=True, ) name = models.CharField(max_length=60) birthplace = models.ForeignKey('myapp.City', related_name="person_birthplace") is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) objects = PersonaManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['name', 'birthplace'] </code></pre> <p>How can I get the foreign key working on my user model on a required field? Thank you.</p> <p>Edit: Traceback from <code>manage.py createsuperuser --traceback</code>:</p> <pre><code>Traceback (most recent call last): File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv self.execute(*args, **options.__dict__) File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/core/management/base.py", line 252, in execute output = self.handle(*args, **options) File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 112, in handle user_data[field_name] = field.clean(raw_value, None) File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 211, in clean self.validate(value, model_instance) File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1014, in validate using = router.db_for_read(model_instance.__class__, instance=model_instance) File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/db/utils.py", line 142, in _route_db return hints['instance']._state.db or DEFAULT_DB_ALIAS AttributeError: 'NoneType' object has no attribute '_state' AttributeError: 'NoneType' object has no attribute '_state' </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload