1

please help to solve this problem

I expanded in django1.6 User model as follows:

class UserProfile(User):    
        ......
        ......

    family = models.CharField(
        max_length=30, 
        blank=True,
    )
    skype = models.CharField(
        max_length=50, 
        blank=False,
    )
    email_address = models.EmailField(
        max_length=50, 
        blank=True,
    )   

    .....
    ....

    objects = UserManager()

resulting in adminpanel appeared a form with the above fields. after filling the data is stored in the database table "app_userprofile". This table is linked to the table "auth_user" using the foreign key.

the problem is that the table "auth_user" fields "username" and "password" empty . but each user needed.

please tell me how to do so after the new user registration ( of the admin panel and from the site ) data "username" and "password" fell into the table "auth_user"

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
dizzy
  • 29
  • 4

3 Answers3

1

I think you need to use AbstractUser model. If you inherit UserProfile from User model you will get 2 tables with same fields, and when you fill app.UserProfile password field - value doesn`t appear in auth.User table.

Of course you can do like this: https://stackoverflow.com/a/23390975/1761844 but better way - create your own custom user:

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
   ... your custom fields ...

and add to your settings.py

AUTH_USER_MODEL = 'app.CustomUser'

after this you will get user model with your custom fields. You can import it in your apps like this:

from django.contrib.auth import get_user_model
User = get_user_model()

and make foreign keys like this:

owner = models.OneToOneField(settings.AUTH_USER_MODEL)

Link to django documentation: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-django-s-default-user

Community
  • 1
  • 1
zymud
  • 2,221
  • 16
  • 24
0

Use a one to one field, User.profile will create the Profile object the first time user.profile it is accessed:

from django.contrib.auth.models import User
from django.db import models

class Profile(models.Model):
    user = models.OneToOneField(User, unique=True)


User.profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])
Chris Montanaro
  • 16,948
  • 4
  • 20
  • 29
0

First, change UserProfile, like it was mentioned in the previous answer. After that, check this answer: https://stackoverflow.com/a/4565957 as it explains how to combine user and user profile forms on a single admin page. Then you will be able to add both user and his/her profile with a single save.

There is also other way to solve your problem: you can define a custom user model with all the fields you need, combining username, password, family and other data in a single table. Check out this page for reference: https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#specifying-a-custom-user-model.

Community
  • 1
  • 1
piotrekw
  • 237
  • 1
  • 8