1

I need to create custom users in my app.

In the example given in the doc

class CustomUser(models.Model):
    user = models.OneToOneField(User)
    #custom fields

a user must exists before creating a CustomUser.

What I want to do is to create automatically a User when I create a CustomUser.

In the CustomUser admin (only visible by the superuser), I'd like to have only the custom fields and a few fields from the User model, as well as some form to allow the superuser to change the password for existing instance.

Anybody could help?

jul
  • 36,404
  • 64
  • 191
  • 318
  • refer this link http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django – irshad Jun 05 '12 at 12:48

1 Answers1

3

The first part of your question is easy, you can use a signal:

def create_custom_user(sender, instance, created, **kwargs):
    if created:
        custom_user, created = CustomUser.objects.get_or_create(user=instance)

post_save.connect(create_custom_user, sender=User)

As for the second part, theres already a change password form in the admin. To filter out the displayed fields you can create a CustomUserAdmin and register it together with the model. It's pretty self explaining in the django docs.

django docs: list_display

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • But if I understand well, this creates a CustomUser when a User is created. I want it the other way round. I want to have an admin to create a CustomUser, which automatically creates a User. – jul Jun 05 '12 at 12:56
  • Then do it visa versa and set your user field to blank=True and null=True – Hedde van der Heide Jun 05 '12 at 12:59