G.Bar
G.Bar

Reputation: 115

Using Django User Model

I've seen people using the default django user model as a foreign key in two ways:

1)

from django.contrib.auth.models import User
user = models.ForeignKey(User)

2)

user = models.ForeignKey('auth.User')

but when implementing one-to-one relation I've only seen:

from django.contrib.auth.models import User
user = models.ForeignKey(User)

I have two questions regarding this:

1) Are the two ways to define Foreign Keys practically the same?

2) Can you use user = models.OneToOneField('auth.user')?

Upvotes: 1

Views: 232

Answers (1)

scharette
scharette

Reputation: 9977

Both works. But, the confusion comes from the fact that 'auth.user' was used before the add of AUTH_USER_MODEL in Django 1.5.

Now, in your code I would actually recommend to use neither. instead follow Django recommendation and use settings.AUTH_USER_MODEL

user = models.ForeignKey(settings.AUTH_USER_MODEL)

or

user = models.OneToOneField(settings.AUTH_USER_MODEL)

This will avoid that your code will stop working in projects where the AUTH_USER_MODEL setting has been changed to a different user model.

Upvotes: 1

Related Questions