所以我试图修改我的模型,现在我无法摆脱这个错误。我试图删除我的所有数据,但它没有帮助。我认为它与我的数据库有关,但在此之前,所有事情都可以正常工作。我收到这个错误:
(1054,“字段列表中的未知栏'courses_course.course_category_id'”)
这是我的模特:
class StudyProgramme(models.Model):
department = models.ForeignKey('Department', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
studies_type = models.IntegerField(choices=((0, "Bachelor Studies"),
(1, "Master Studies"),
(2, "PhD Studies"),
(3, "Integrated Studies")))
duration = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
class Meta:
verbose_name = "Study Programme"
verbose_name_plural = 'Study Programmes'
def __str__(self):
return self.name
class Course(models.Model):
study_programme = models.ForeignKey('StudyProgramme', on_delete=models.CASCADE)
course_category = models.ForeignKey('CourseCategory', on_delete=models.CASCADE, default='',
related_name='categories')
name = models.CharField(max_length=50)
ects = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
description = models.TextField()
year = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
semester = models.IntegerField(choices=((1, "1"),
(2, "2"),
), default=None)
slug = models.SlugField(max_length=140, unique=True)
def __str__(self):
return self.name
def _get_unique_slug(self):
slug = slugify(self.name)
unique_slug = slug
num = 1
while Course.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, num)
num += 1
return unique_slug
def save(self, *args, **kwargs):
if not self.slug:
self.slug = self._get_unique_slug()
super().save()
class CourseCategory(models.Model):
course_category = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.course_category
class Lecture(models.Model):
course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures')
lecture_title = models.CharField(max_length=100)
content = models.TextField()
link = models.URLField(blank=True)
files = models.ManyToManyField('FileUpload', blank=True)
def __str__(self):
return self.lecture_title
class FileUpload(models.Model):
file = models.FileField(upload_to='documents')
def file_link(self):
if self.file:
return "<a href='%s'>download</a>" % (self.file.url,)
else:
return "No attachment"
file_link.allow_tags = True
file_link.short_description = 'File Download'
答案 0 :(得分:0)
我已经通过从mysql shell中删除数据库然后重新创建它来解决了这个问题。
答案 1 :(得分:0)
对模型进行任何更改后,您需要运行这两个命令才能将它们反映到数据库中。
python manage.py makemigrations
和
python manage.py migrate
将您的模型类反映到数据库是一个两步过程。 从Django Documentation了解有关迁移的详情。