[Fixed]-Django Composite Username

1👍

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)

Leave a comment