birdy
birdy

Reputation: 9646

How to extend the user model to add more fields to it

I am using the default User model that django provides. But I would like to add more fields to it that are specific to my application.

I've done the following but it doesn't seem to be working. I am trying to add a field called company_name and to test whether it works I try to update it using the rest API.

last line of settings.py:

ROOT_URLCONF = 'demo.urls'
WSGI_APPLICATION = 'demo.wsgi.application'
...
AUTH_USER_MODEL = "demo.UserProfile"

Models.py:

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

class UserProfile(AbstractUser):
    company_name = models.CharField(max_length=50)

I'm not sure whether it is setting the company_name property but since it isn't returning it..I'm assuming what I've done doesn't work.

enter image description here

Upvotes: 2

Views: 79

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600006

But you haven't extended the User model at all, you've created a separate model which links to it via a 1-1 relation. So you shouldn't expect a JSON dump of the User model to include your profile fields.

I don't know what framework you are using to generate that REST output, but maybe you can extend the serializer to include the related fields. But a better approach would be to actually use a custom user model: rather than linking User to a Profile, extend the AbstractUser class with your own fields and tell Django to use your model in place of the default. This is fully documented.

Upvotes: 3

Related Questions