django.db.utils文件.ProgrammingError:关系“dashboard\u menugroup”不存在

2021-09-16 23:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这些模型:

# coding:utf-8
from django.db import models
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _

class MenuGroup(models.Model):
    name = models.CharField(_('nome'), max_length=30, blank=False, primary_key=True)
    slug = models.SlugField(_('slug'), blank=True, null=True, db_index=True)

    class Meta:
        app_label = 'dashboard'
        verbose_name = _('gruppo')
        verbose_name_plural = _('gruppi')
        ordering = ('name',)

    def __str__(self):
        return self.name

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


class MenuProduct(models.Model):
    name = models.CharField(max_length=30)
    ingredients = models.CharField(max_length=250)
    price = models.FloatField(null=True, blank=True, default=0.0)
    group = models.ForeignKey(MenuGroup, on_delete=models.CASCADE)

    class Meta:
        app_label = 'dashboard'
        verbose_name = _('prodotto')
        verbose_name_plural = _('prodotti')
        ordering = ('name',)

    def __str__(self):
        return self.name


class Order(models.Model):
    price = models.FloatField(null=True, blank=True, default=0.0)
    date = models.DateTimeField(auto_now=False)
    status = models.BooleanField(default=False)

    class Meta:
        app_label = 'dashboard'
        verbose_name = _('ordine')
        verbose_name_plural = _('ordini')
        ordering = ('date',)

    def __str__(self):
        return self.date


class OrderHasMenuProduct(models.Model):
    product = models.ForeignKey(MenuProduct, on_delete=models.CASCADE)
    order = models.ForeignKey(Order, on_delete=models.CASCADE)

假设我们要重置数据库并启动以下命令:

$ find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
$ find . -path "*/migrations/*.pyc"  -delete

然后我们启动命令来重新创建模式:

$ python manage.py makemigrations

我得到如下输出: “仪表板”的迁移:

仪表板/迁移/0001_初始.py -创建模型菜单组 -创建风管模型 -创建模型订单 -创建模型OrderHasMenuProduct

然后我启动:

$ python manage.py migrate

我得到如下输出:

运行迁移: 没有要应用的迁移。你知道吗

使用数据库的djangoapi,我将一些记录插入到MenuGroup表中,然后打开一个视图来显示所有记录。你知道吗

我的观点:

from django.views.generic.list import ListView
class MenuView(ListView):
    model = MenuGroup
    context_object_name = "all_menu_groups"
    template_name = 'dashboard/menu.html'

    def get_context_data(self, **kwargs):
        context = super(MenuView, self).get_context_data(**kwargs)
        context['page_title'] = _('Menu')
        return context

当我尝试发送页面时,我得到以下错误:

django.db.utils.ProgrammingError: relation "dashboard_menugroup" does not exist
LINE 1: SELECT (1) AS "a" FROM "dashboard_menugroup" WHERE "dashboar...
                           ^