简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM