user3034246
user3034246

Reputation:

How to add custom filed to Django User Model

I am working on the project using django 1.9.

I need to add a field to the user model 'Auth_user' table, the field which i want can be another primary key and act here as foreign key in the 'auth_user'.

I searched a lot but fails. Can any buddy provide me some example how to achieve this like how to to add fields to 'auth_user'

Upvotes: 1

Views: 695

Answers (3)

hurturk
hurturk

Reputation: 5454

You can substitute the user model entirely as described in doc. Here is an example:

AUTH_USER_MODEL = 'myapp.MyUser'

to your settings.py, and add following to your model:

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    another_object = models.ForeignKey(OtherModel..

Upvotes: 1

Javier Campos
Javier Campos

Reputation: 365

just make a new moel with a user foreign key

class Post (models.Model):
   title = models.CharField(max_length=80)
   slug= models.SlugField(unique=True)
   content = models.TextField()
   user_creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

there you can use as primary key the id of the post, or the slug (unique), and it can be linked to a user, if you need one to one relationship see this or many to many relationship see this

Upvotes: 0

Related Questions