我有以下模型:
# 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
我获得以下输出: “仪表板”的迁移:
dashboard / migrations / 0001_initial.py -创建模型MenuGroup -创建模型MenuProduct -创建模型订单 -创建模型OrderHasMenuProduct
然后我启动:
$ python manage.py migrate
我获得以下输出:
正在运行的迁移: 没有要申请的迁移。
使用用于数据库的Django API,我将一些记录插入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...
^
答案 0 :(得分:0)
仅删除迁移文件还不够。您还必须删除数据库。
编辑:实际上,您必须删除一些迁移表。您可以使用未记录的Django模型来这样做:MigrationRecorder.Migration.objects.all().delete()
对于这种情况,我有一个管理命令包,但尚未经过很好的测试以用于生产。