Reputation: 4591
Is it possible to use two or more fields configured to be unique together as a username in Django? Is this a dumb idea?
Eg;
class MyUser(AbstractBaseUser, PermissionsMixin):
user_id = models.CharField(max_length=255)
backend = models.CharField(max_length=255)
username = backend + user_id # ???
class Meta:
unique_together = (
('user_id', 'backend')
)
Upvotes: 0
Views: 158
Reputation: 73498
You could make 'username'
a property
. But since I assume you want it to be the USERNAME_FIELD
and be able to query the db with it, I'd do something like:
class MyUser(AbstractBaseUser, PermissionsMixin):
user_id = models.CharField(max_length=255)
backend = models.CharField(max_length=255)
username = models.CharField(max_length=510, editable=False)
class Meta:
unique_together = (
('user_id', 'backend')
)
def save(self, *args, **kwargs):
self.username = self.backend + self.user_id
super(MyUser, self).save(*args, **kwargs)
Upvotes: 1