0

I am modeling a person like so:

class Person(models.Model):
    """ Represent a person who has credentials. The person may have
    devices and/or accessories. """ 

    #basic information
    name = models.CharField(max_length=50, unique=True)
    email = models.CharField(max_length=50, unique=True)
    phone_number = PhoneNumberField(unique=True)

But I want to be able to add something like:

/* create admin user with a group priveledge*/
user_account = #link to user account here

I'm doing this because as I add people objects, I want them to have an account with certain privelidges.

Thanks to anyone who can help..

Matt M
  • 149
  • 2
  • 4
  • 17

2 Answers2

1

It globally depends on the version of Django you're using, since User Models and Inheritance have changed with Django 1.5.

You might want to take a look at the Django Official Documentation : https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

Cheers, K.

EDIT 19:30 CEST : You should be taking care of the switch in the right top of the Django's Documentation website, just to be sure you're looking at the right documentation (i.e. the documentation that concerns the Django's version you're using). As from 1.5, Django added an awesome thing : not just "relate" to the User model, but also Extends it. https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model .

And, as I guess that you want to add user via the automatic admin, you should take care of this paragraph too : https://docs.djangoproject.com/en/dev/topics/auth/customizing/#custom-users-and-django-contrib-admin

The only thing is that you must run Django 1.5+ to do that. Django 1.4 and lesser just let you add a related model, wich is not so bad yet :).

Koreth
  • 681
  • 5
  • 15
  • I appreciate the help but it states here: These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate. I want an admin account to be created. I looked on the post_save page and really there's nothing for me to follow there.. – Matt M May 07 '13 at 16:49
  • Yep. I understand better the ask, now :) I complete my answer with an edit. – Koreth May 07 '13 at 17:23
  • I'm still not getting the answer. Let me rephrase: I have my person model and I want to create a user profile using the name, email, and the password as their phone number. How do I: 1) make a user profile with that information. 2) Where do I put it? Do I put it in the models.py? I am experimenting with: account = User.objects.create_user(str(name), str(email), str(identification)) – Matt M May 07 '13 at 18:23
0

Can we say that what you want is just a User with a few more informations ?

Or is there a situation where you'll say

  • "A person may not be a user" and/or
  • "A user may not be a person"

What you describe here is possible with the simple system of the foreign key. Make a new model Person like this :

class Person(models.Model):
  ... some infos here ...
  user = model.ForeignKey(User)

And then, you'll just have to create the User and then, the Person. There's many ways you can tell Django to create automatically the Person object when the User object is created. This blogpost could drive you the right way : http://www.turnkeylinux.org/blog/django-profile

Maybe you should follow it, and then explain us exactly what you miss ?

Cheers, again.

K.

Koreth
  • 681
  • 5
  • 15
  • No. I don't want this. I want the user and person created at the same time when I create the person. – Matt M May 07 '13 at 19:18
  • So you might want take a look at http://stackoverflow.com/questions/11488974/django-create-user-profile-on-user-creation – Koreth May 07 '13 at 19:51