Django 1.8操作错误:没有这样的表

时间:2015-10-07 18:37:02

标签: python django django-models django-admin

我发现很多类似的问题/答案建议使用" migrate"和#34; makemigrations"但那没用。我使用python 2.7和pycharm 4.5.4作为IDE。 当我为产品图像域创建一个新模型时,注册到管理员并且我运行了#34; makemigrations"然后"迁移"然后我在/ admin / products / productimage /得到了这个错误OperationalError 没有这样的表:products_productimage。我在这里错过了什么?下面是

model.py

    from django.db import models
    from django.core.urlresolvers import reverse
    from django.db.models.signals import post_save


    # Create your models here.

    class ProductQuerySet(models.query.QuerySet):
        def active(self):
            return self.filter(active=True)


    class ProductManager(models.Manager):
        def get_queryset(self):
            return ProductQuerySet(self.model, using=self.db)

        def all(self, *args, **kwargs):
            return self.get_queryset().active()


    class Product(models.Model):
        title = models.CharField(max_length=120)
        description = models.TextField(blank=True, null=True)
        price = models.DecimalField(decimal_places=2, max_digits=10)
        active = models.BooleanField(default=True)
        objects = ProductManager()

        def __unicode__(self):
            return self.title

        def get_absolute_url(self):
            return reverse("product_detail", kwargs={"pk": self.pk})
            # OR use this- return "/product/%s"%(self.pk)


    class Variation(models.Model):
        product = models.ForeignKey(Product)  ##this means each Variation is related to single product
        title = models.CharField(max_length=120)
        price = models.DecimalField(decimal_places=2, max_digits=10)
        sale_price = models.DecimalField(decimal_places=2, max_digits=10, null=True, blank=True)
        active = models.BooleanField(default=True)
        inventory = models.IntegerField(null=True, blank=True)  # default=-1 means unlimited

        def __unicode__(self):
            return self.title

        def get_price(self):
            if self.sale_price is not None:
                return self.sale_price
            else:
                return self.price

        def get_absolute_url(self):
            return self.product.get_absolute_url()


    # for post save receiver


    def product_saved_receiver(sender, instance, created, *args, **kwargs):
        # sender=modelclass, instance=actual instance being saved,created=boolean true if record was created
        product = instance
        variations = product.variation_set.all()
        if variations.count() == 0:
            new_var = Variation()
            new_var.product = product
            new_var.title = "Default"
            new_var.price = product.price
            new_var.save()


    post_save.connect(product_saved_receiver, sender=Product)


    # product image
    # you need to install python pillow library to support. it checks if file uploaded is actually an image and checks extension
    class ProductImage(models.Model):
        product = models.ForeignKey(Product)
        image = models.ImageField(upload_to='products/')

        def __unicode__(self):
            return self.product.title

admin.py

    from django.contrib import admin
    from .models import Product, Variation, ProductImage

    # Register your models here.
    admin.site.register(Product)
    admin.site.register(Variation)
    admin.site.register(ProductImage)

输出makemigrations并迁移

manage.py@dj > makemigrations
    "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\bin\runnerw.exe" C:\Anaconda\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\django_manage.py" makemigrations C:/Users/user1/PycharmProjects/dj
    No changes detected

    Process finished with exit code 0
    manage.py@dj > migrate
    "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\bin\runnerw.exe" C:\Anaconda\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\django_manage.py" migrate C:/Users/user1/PycharmProjects/dj
    Operations to perform:
      Synchronize unmigrated apps: staticfiles, messages, crispy_forms
      Apply all migrations: sessions, admin, sites, auth, contenttypes, products, registration, newsletter
    Synchronizing apps without migrations:
      Creating tables...
        Running deferred SQL...
      Installing custom SQL...
    Running migrations:
      No migrations to apply.

    Process finished with exit code 0
浏览器上的

我得到以下错误enter image description here

2 个答案:

答案 0 :(得分:2)

您是否尝试migrate <yourapp> zero --fake然后再次migrate <yourapp>?这对我有用,我有同样的问题。

答案 1 :(得分:0)

如果您不关心丢失数据,可以删除SQLite3文件并运行&#39; makemigrations&#39;并且&#39;迁移&#39;命令再次。您可能还需要重新创建超级用户(python manage.py createsuperuser)