Shawn Taylor
Shawn Taylor

Reputation: 1454

Django User table as Foreign Key

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?

Upvotes: 4

Views: 2400

Answers (1)

Sam Dolan
Sam Dolan

Reputation: 32532

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.

Upvotes: 9

Related Questions