0
我有一個型號的產品,它有一個產品型號的子型號差異。現在,我正在嘗試添加產品的商店模型頂部,並希望獲得由創建商店的店主上傳的產品。但我得到一個錯誤。以下是我的代碼:Django ORM不適用於型號
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=20)
active = models.BooleanField(default=True)
categories = models.ManyToManyField('Category', blank=True)
default = models.ForeignKey('Category', related_name='default_category', null=True, blank=True)
hits = models.ManyToManyField(HitCount, blank=True)
hitcounts = GenericRelation(HitCount, content_type_field='content_type', object_id_field='object_pk',)
objects = ProductManager()
class Meta:
ordering = ["-title"]
class Variation(models.Model):
product = models.ForeignKey(Product)
title = models.CharField(max_length=120)
price = models.DecimalField(decimal_places=2, max_digits=20)
sale_price = models.DecimalField(decimal_places=2, max_digits=20, null=True, blank=True)
active = models.BooleanField(default=True)
inventory = models.IntegerField(null=True, blank=True) #refer none == unlimited amount
def __unicode__(self):
return self.title
class Shop(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=False, blank=False)
products = models.ManyToManyField(Product)
name = models.CharField(max_length=120)
image = models.ImageField(upload_to=image_upload_to_shop, null=True)
location = models.CharField(max_length=120)
def __unicode__(self):
return self.name
錯誤,我得到的是,當我嘗試添加店:
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/products/shop/add/
Django Version: 1.8.5
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'products',
'orders',
'carts',
'newsletter',
'crispy_forms',
'registration',
'colorfield',
'hitcount')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\contrib\admin\options.py" in wrapper
616. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\contrib\admin\sites.py" in inner
233. return view(request, *args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\contrib\admin\options.py" in add_view
1516. return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\utils\decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\utils\decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\utils\decorators.py" in inner
145. return func(*args, **kwargs)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\contrib\admin\options.py" in changeform_view
1468. self.save_related(request, form, formsets, not add)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\contrib\admin\options.py" in save_related
1100. form.save_m2m()
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\forms\models.py" in save_m2m
102. f.save_form_data(instance, cleaned_data[f.name])
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\fields\related.py" in save_form_data
2590. setattr(instance, self.attname, data)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\fields\related.py" in __set__
1261. manager.clear()
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\fields\related.py" in clear
998. self.through._default_manager.using(db).filter(filters).delete()
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\query.py" in delete
537. collector.delete()
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\deletion.py" in delete
292. qs._raw_delete(using=self.using)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\query.py" in _raw_delete
549. sql.DeleteQuery(self.model).delete_qs(self, using)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\sql\subqueries.py" in delete_qs
78. self.get_compiler(using).execute_sql(NO_RESULTS)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
840. cursor.execute(sql, params)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\backends\utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\backends\utils.py" in execute
64. return self.cursor.execute(sql, params)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\utils.py" in __exit__
97. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\backends\utils.py" in execute
64. return self.cursor.execute(sql, params)
File "C:\Users\Shazia\ecommerce\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
318. return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/products/shop/add/
Exception Value: no such table: products_shop_products
這裏是我的我的應用程序遷移表:
001-Initial.py
from __future__ import unicode_literals
from django.db import migrations, models
import colorfield.fields
import products.models
class Migration(migrations.Migration):
dependencies = [
('hitcount', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('title', models.CharField(unique=True, max_length=120)),
('slug', models.SlugField(unique=True)),
('description', models.TextField(null=True, blank=True)),
('active', models.BooleanField(default=True)),
('timestamp', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='color_product',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('color', colorfield.fields.ColorField(default=b'#FF0000', max_length=10)),
],
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('title', models.CharField(max_length=120)),
('description', models.TextField(null=True, blank=True)),
('price', models.DecimalField(max_digits=20, decimal_places=2)),
('active', models.BooleanField(default=True)),
('categories', models.ManyToManyField(to='products.Category', blank=True)),
('default', models.ForeignKey(related_name='default_category', blank=True, to='products.Category', null=True)),
('hits', models.ManyToManyField(to='hitcount.HitCount', blank=True)),
],
options={
'ordering': ['-title'],
},
),
migrations.CreateModel(
name='ProductFeatured',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('image', models.ImageField(upload_to=products.models.image_upload_to_featured)),
('title', models.CharField(max_length=120, null=True, blank=True)),
('text', models.CharField(max_length=220, null=True, blank=True)),
('text_right', models.BooleanField(default=False)),
('text_css_color', models.CharField(max_length=6, null=True, blank=True)),
('show_price', models.BooleanField(default=False)),
('make_image_background', models.BooleanField(default=False)),
('active', models.BooleanField(default=True)),
('product', models.ForeignKey(to='products.Product')),
],
),
migrations.CreateModel(
name='ProductImage',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('image', models.ImageField(upload_to=products.models.image_upload_to)),
('product', models.ForeignKey(to='products.Product')),
],
),
migrations.CreateModel(
name='Shop',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=120)),
('image', models.ImageField(null=True, upload_to=products.models.image_upload_to_shop)),
('location', models.CharField(max_length=120)),
('products', models.ManyToManyField(to='products.Product')),
],
),
migrations.CreateModel(
name='size_product',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('size', models.CharField(max_length=10, choices=[(b'XS', b'XS'), (b'S', b'S'), (b'SM', b'SM'), (b'M', b'M'), (b'L', b'L'), (b'XL', b'Xl'), (b'XXL', b'XXL')])),
('price', models.DecimalField(max_digits=20, decimal_places=2)),
('product', models.ForeignKey(to='products.Product')),
],
),
migrations.CreateModel(
name='Variation',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('title', models.CharField(max_length=120)),
('price', models.DecimalField(max_digits=20, decimal_places=2)),
('sale_price', models.DecimalField(null=True, max_digits=20, decimal_places=2, blank=True)),
('active', models.BooleanField(default=True)),
('inventory', models.IntegerField(null=True, blank=True)),
('product', models.ForeignKey(to='products.Product')),
],
),
migrations.AddField(
model_name='color_product',
name='product',
field=models.ForeignKey(to='products.Product'),
),
]
002_shop_user.py
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('products', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='shop',
name='user',
field=models.ForeignKey(default=1, to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]
您沒有遷移數據庫。 –
錯誤很明顯,中間表不存在。你運行makemigrations並遷移? –
運行命令'python manage.py makemigrations'和'python manage.py migrate' –