Django 表gigpost_类别没有名为
我正试图建立一个简单的博客,我面临着这个问题。当我尝试在我的管理面板中添加模型时,它会显示此错误 代码如下: models.pyDjango 表gigpost_类别没有名为,django,django-models,Django,Django Models,我正试图建立一个简单的博客,我面临着这个问题。当我尝试在我的管理面板中添加模型时,它会显示此错误 代码如下: models.py from django.db import models from django.utils import timezone from django.conf import settings # Create your models here. class Category(models.Model): titled=models.CharField(max
from django.db import models
from django.utils import timezone
from django.conf import settings
# Create your models here.
class Category(models.Model):
titled=models.CharField(max_length=100,default='')
def __str__(self):
return self.title
class Gigpost(models.Model):
title=models.CharField(default='',max_length=100,blank=False)
user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=False)
categories=models.OneToOneField(Category,on_delete=models.PROTECT,default='',blank=False)
published_at=models.DateTimeField(auto_now_add=True)
description=models.TextField(default='',max_length=None,blank=False)
mainphoto=models.ImageField(default='')
photo=models.ImageField()
def __str__(self):
return self.title
class Comment_area(models.Model):
user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=False)
comment=models.TextField(max_length=None,default='')
commented_at=models.DateTimeField(auto_now_add=True)
管理员
from django.contrib import admin
from .models import Gigpost , Category
# Register your models here.
admin.site.register(Gigpost)
admin.site.register(Category)
错误消息:
OperationalError at /admin/gigpost/category/add/
table gigpost_category has no column named titled
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/gigpost/category/add/
Django Version: 2.2.5
Exception Type: OperationalError
Exception Value:
table gigpost_category has no column named titled
已修复您的打字错误等。请尝试以下操作: 型号.py
from django.db import models
from django.utils import timezone
from django.conf import settings
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
title = models.CharField(max_length=100, default='')
def __str__(self):
return self.title
class Gigpost(models.Model):
title = models.CharField(default='',max_length=100)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
categorie=models.ForeignKey(Category,on_delete=models.SET_NULL, null=True)
published_at = models.DateTimeField(auto_now_add=True)
description = models.TextField(default='')
mainphoto = models.ImageField(blank=True)
photo = models.ImageField(blank=True)
def __str__(self):
return self.title
class Comment_area(models.Model):
user=models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
comment=models.TextField(default='')
commented_at=models.DateTimeField(auto_now_add=True)
在对models.py进行任何更改后,不要忘记运行makemigrations
和migrate
您还应该检查迁移文件是否已创建。您可能需要重新启动服务器。最近是否添加了标题字段?您是否创建了新的迁移并运行了它?我在创建模型时添加了标题,我只是通过将其更改为“titled”来重命名类别的标题,然后运行迁移您是否执行了python manage.py makemigrations
?您的代码有几个错误。如果不是从抽象模型继承,为什么要创建抽象模型?如果只有一个名为titled的字段,它应该如何返回self.title?运行makemigrations和migrate将模型上的更改迁移到数据库。@errorinpersona我做了更改并运行了迁移,但仍然是相同的错误感谢您的时间和评论,但正如您注意到我做了costum用户一样,我现在就试试。。PS:我尝试了另一个项目,它成功了,我想问题出在数据库上,你知道吗?它成功了,我刚刚删除了所有的迁移文件夹和db.sqlite3,然后再次运行迁移,谢谢你的帮助!