我试图在我的 models.py 文件中将用户模型作为 ForeignKey 的参数传递,但我收到了错误消息TypeError: int() argument must be a string or a number, not 'User'
。
这是我的文件,请告诉我我做错了什么:
模型.py
from django.db import models
class Lesson(models.Model):
name = models.CharField(max_length=30)
author = models.ForeignKey('auth.User')
description = models.CharField(max_length=100)
yt_id = models.CharField(max_length=12)
upvotes = models.IntegerField()
downvotes = models.IntegerField()
category = models.ForeignKey('categories.Category')
views = models.IntegerField()
favorited = models.IntegerField()
def __unicode__(self):
return self.name
populate_db.py
from django.contrib.auth.models import User
from edu.lessons.models import Lesson
from edu.categories.models import Category
users = User.objects.all()
cat1 = Category(1, 'Mathematics', None, 0)
cat1.save()
categories = Category.objects.all()
lesson1 = Lesson(1, 'Introduction to Limits', users[0], 'Introduction to Limits', 'riXcZT2ICjA', 0, 0, categories[0], 0, 0)
lesson1.save() # Error occurs here