views:

45

answers:

1

I want username field automatically filled with this:

username = email[0] + str(id)

where id is the id of the user object

Is it possible ?

Thanks :)

A: 

You can use hooks to achieve this.

It would be something like this (not tested):

from django.db import models

  class User(models.Model):
    email = model.EmailField()
    username = models.CharField(max_length=80)

    def save(self):
      if not self.id:
           self.username = ''
           super(User, self).save()
      self.username = "%i%s" % (self.id, self.email)
      super(User, self).save()
jbochi
self.id doen't exist before saving an object. I tried this but I get error :(
xRobot
You're right! I have edit my answer. It should work now. Sorry for not testing the code.
jbochi
thank you.... :)
xRobot