views:

17

answers:

1

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?

+1  A: 

Since you are automatically creating an UserProfile using a signal you can explicitly set the User instance's username as the UserProfile subdomain. For e.g.

def create_user_profile(sender, instance, **kwargs):
    profile = UserProfile(user = instance, subdomain = instance.username, ...)
    profile.save()

Another way to do this would be to override the save() method such that if no subdomain is supplied you can set it to user.username.

Manoj Govindan
Works perfect, thanks!
Sergey Basharov