【发布时间】:2016-01-05 03:23:29
【问题描述】:
我发现很多类似的问题/答案建议使用“迁移”和“makemigrations”,但没有奏效。我使用 python 2.7 和 pycharm 4.5.4 作为 IDE。 当我为产品图像字段创建新模型时,向管理员注册并运行“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 和 migrate 的输出
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
在浏览器上我得到以下错误
【问题讨论】:
-
你能在运行这些命令时显示它们的输出吗?
-
@ThomasOrozco 我已经更新了帖子。欢呼
-
可能不相关,但您的意思是让
def product_saved_receiver...和post_save.connect...进一步缩进一级? -
只是为了确认一下:在明确指定您的应用程序时,您是否获得了任何迁移?
makemigrations products -
@mfcovington 我认为缩进是正确的。我正在使用 django admin 上传图片并收到此错误
标签: python django django-models django-admin