I use UserProfile model for keeping additional info about user and one of the fields is subdomain variable that is by default should be set to username. Here is UserProfile model:
class UserProfile(models.Model):
theme = models.ForeignKey(Theme, unique=True, null=True)
user = models.ForeignKey(User, unique=True)
subdomain = models.CharField(default=self.user.username, max_length=30)
I implemented Django signals feature to create UserProfile model instance automatically on user creation. Now I need to set subdomain variable to username. How can I do this?
Since you are automatically creating an
UserProfileusing a signal you can explicitly set theUserinstance’susernameas theUserProfilesubdomain. For e.g.Another way to do this would be to override the
save()method such that if no subdomain is supplied you can set it touser.username.