I'm creating a new model called Tickets which I want to ensure always has a valid userID assigned to it.
I'm using AUTH_PROFILE_MODULE
to setup the profile which also gives the error NameError: name 'User' is not defined when I try to run syndb.
How do I setup a foreign key to make sure this always is the case?
## tickets/models.py
class Ticket(models.Model):
user = models.ForeignKey(User,)
# More model stuff.
# accounts/models.py
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
so the problem was I was missing an import on my models.
from django.contrib.auth.models import User
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,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With