/ admin / budget / expense / add /中的OperationalError

时间:2019-09-15 20:15:00

标签: python django

当尝试在http://localhost:8000/admin/budget/expense/add/上节省新费用时,我收到一条OperationalError消息,内容为:

没有这样的表格:budget_expense

我认为可能与我的models.py文件的设置有关:

from django.utils.text import slugify

# Create your models here.

class Project(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100, unique=True, blank=True)
    budget = models.IntegerField()

    def save(self, * args, **kwargs):
        self.slug = slugify(self.name)
        super(Project, self).save(*args, **kwargs)

class Category(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

class Expense(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

有关更多上下文,这是错误消息的样子:

Request Method: POST
Request URL:    http://localhost:8000/admin/budget/expense/add/
Django Version: 2.2.5
Exception Type: OperationalError
Exception Value:    
no such table: budget_expense
Exception Location: C:\Python37\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 383
Python Executable:  C:\Python37\python.exe
Python Version: 3.7.4
Python Path:    
['C:\\Users\\kobby\\Documents\\financio',
 'C:\\Python37\\python37.zip',
 'C:\\Python37\\DLLs',
 'C:\\Python37\\lib',
 'C:\\Python37',
 'C:\\Python37\\lib\\site-packages']
Server time:    Sun, 15 Sep 2019 20:00:54 +0000

我可以很好地连接到Django管理员,并且已经能够添加“项目”,但是当我要添加“费用”时,就是出现错误了。关于这可能是为什么的任何建议?

2 个答案:

答案 0 :(得分:0)

首先,您必须为添加表(documentation)创建迁移

接下来,您必须运行迁移。它在您的数据库(documentation)中创建表

答案 1 :(得分:0)

感谢帮助人员,Django文档确实被低估了。我在(documentation)中找到了答案。我需要做的就是运行python manage.py migrate --fake-initial,它基本上可以检测到初始迁移并将迁移标记为已应用。