1

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'

3 Answers 3

1

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..
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

thanks for the reply... but this is something that i don't want.
0

Maybe you just need to extend de model user

https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-the-existing-user-model

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.