AbstractBaseUser with a foreignkey...

115 views
Skip to first unread message

Vanni Brutto

unread,
Nov 7, 2013, 8:56:03 AM11/7/13
to django...@googlegroups.com
i'm trying to use AbstractBaseUser to extend the user model...

i used the code found on django pages, the only difference is that i use a foreignkey to "Sistema" models.

my changes was:

[...]
class Sistema(models.Model):
    models.ForeignKey(settings.AUTH_USER_MODEL)
    nome = models.CharField(max_length=80)
    def __unicode__(self):
        return self.nome
    class Meta:
        verbose_name_plural = "Sistemi"

[...]

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        db_index=True,
    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    TIPO_CHOICES = (
        ('C', 'Cameriere'),
        ('G', 'Guest'),
        ('A', 'Admin'),
    )
    tipo = models.CharField(max_length=1, choices=TIPO_CHOICES, default="C")
    sistema = models.ForeignKey(Sistema)



when create the db with syncdb i got:
ds\createsuperuser.py", line 116, in handle
    user_data[field_name] = field.clean(raw_value, None)
  File "C:\apps\Python27\Lib\site-packages\django\db\models\fields\__init__.py",
 line 255, in clean
    self.validate(value, model_instance)
  File "C:\apps\Python27\Lib\site-packages\django\db\models\fields\related.py",
line 1189, in validate
    using = router.db_for_read(model_instance.__class__, instance=model_instance
)
  File "C:\apps\Python27\Lib\site-packages\django\db\utils.py", line 250, in _ro
ute_db
    return hints['instance']._state.db or DEFAULT_DB_ALIAS
AttributeError: 'NoneType' object has no attribute '_state'

C:\apps\xampp\htdocs\py\comanda>

Vincenzo Prignano

unread,
Nov 8, 2013, 10:01:40 AM11/8/13
to django...@googlegroups.com
I think the problem here is that when you create the superuser, the related field will not be created.
You should add sistema = models.ForeignKey('Sistema', null=True) or sistema = models.ForeignKey('Sistema', default=1) where 1 is a placeholder 'Sistema' object that you have created.
I suggest to just use the null method as it designs a better system.
Reply all
Reply to author
Forward
0 new messages