Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, how to ForeignKey(auth_user)?

Tags:

django

I am a newbie of django. I want to make a ForeignKey to the auth_user table.

I try to do that: user = models.ForeignKey(auth_user) It cause the error: NameError: name 'auth_user' is not defined So, can somebody tell me how to import auth_user.

like image 287
lizs Avatar asked Dec 10 '22 15:12

lizs


1 Answers

Since foreign keys accept strings, you can use the AUTH_USER_MODEL setting in your foreign key.

from django.conf import settings

class MyModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

This works whether you are using the built in User model, or a custom model.

like image 159
Alasdair Avatar answered Jan 30 '23 11:01

Alasdair