Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django User table as Foreign Key

Tags:

python

django

web

I'm a new user to Django and am just starting my first app, "myapp".

In my models.py for my app I have the following model:

class Subcalendar(models.Model):
  user = models.ForeignKey(User)

But when I try running:

python manage.py sql myapp

I get an error stating:

NameError: name 'User' is not defined

Do I need to make my own model for User or is there another way of calling it in my model's foreign key field?

like image 281
Shawn Taylor Avatar asked Jan 15 '23 00:01

Shawn Taylor


1 Answers

You need import the User model at the top of your file

 from django.contrib.auth.models import User

so python can resolve the reference.

like image 179
Sam Dolan Avatar answered Jan 19 '23 10:01

Sam Dolan