[Solved]-How do I create a foreign key to the User table in Django?

25👍

so the problem was I was missing an import on my models.

from django.contrib.auth.models import User

10👍

For future reference, according to the official Django documentation, it is recommended to reference the User model like this:

from django.conf import settings
from django.db import models


class Article(models.Model):
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
👤kas

Leave a comment