我正在建立一家电子商务商店,并且正在从数据库中感染物品并在模板上进行渲染。但是我有这个问题。我正在尝试呈现动态网址,并抛出一些错误。我曾尝试调试它,但无济于事。
这是它(在浏览器上)显示的错误:
OperationalError at /
no such column: minishop_app_products.user_id
以下是一些代码段:
View.py
from django.shortcuts import render
from .models import *
from django.http import HttpResponse
def index(request):
header = headerSlider.objects.all()
new_item = Products.objects.all()
context = {
'header': header,
'new_item': new_item,
}
return render(request, 'index.html', context)
def product_single(request, pk):
single_product = Products.objects.get(pk=id)
print(single_product)
sizes = Size.objects.all()
context = {
'single_product':single_product,
'sizes': sizes
}
return render(request, 'product-single.html', context)
Models.py
from django.db import models
from django.contrib.auth.models import User
class headerSlider(models.Model):
title = models.CharField(max_length=200 , null=True)
description = models.CharField(max_length=400 , null=True)
category = models.CharField(max_length=200 , null=True)
image = models.ImageField(null=True, blank=False)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'Head Slider'
class Size(models.Model):
size = models.CharField(max_length=50)
def __str__(self):
return self.size
class Products (models.Model):
product_title = models.CharField(max_length=200 , null=True)
product_price = models.FloatField()
product_category = models.CharField(max_length=200 , null=True)
product_image = models.ImageField(null=True, blank=False)
detail_title1 = models.CharField(max_length=200 , null=True)
detail_title2 = models.CharField(max_length=200 , null=True)
detail_description1 = models.TextField(max_length=500 , null=True)
detail_description2 = models.TextField(max_length=500 , null=True)
quantity_avail = models.IntegerField()
quantity_sold = models.IntegerField()
manufacturer_detail = models.TextField(max_length=500 , null=True)
detail_image1 = models.ImageField(null=True, blank=False)
detail_image2 = models.ImageField(null=True, blank=False)
detail_image3 = models.ImageField(null=True, blank=False)
detail_image4 = models.ImageField(null=True, blank=False)
user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True, blank=True)
size = models.ForeignKey(Size, on_delete=models.CASCADE, default="Medium")
def __str__(self):
return self.product_title
class Meta:
verbose_name_plural = 'Products'
加:我在我的models.py文件中添加了一些字段(并尝试进行迁移)。迁移时,它会抛出此错误:
TypeError: Field 'id' expected a number but got <User: >.
Url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('blog/', views.blog, name='blog'),
path('blog-single/', views.blog_single, name='blog-single'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
path('cart/', views.cart, name='cart'),
path('checkout/', views.checkout, name='checkout'),
path('product/<str:id>', views.product_single, name='product_single'),
path('shop/', views.shop, name='shop'),
]
迁移错误-来自终端
Applying minishop_app.0006_auto_20200920_0938...Traceback (most recent call last):
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1774, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 104, in database_forwards
schema_editor.add_field(
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 189, in _remake_table
self.effective_default(create_field)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 971, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 823, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2388, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/uniquezra/Desktop/Artivate_Reg/regenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
raise e.__class__(
TypeError: Field 'id' expected a number but got <User: >.
0006_auto_20200920_0938
from django.conf import settings
import django.contrib.auth.models
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('minishop_app', '0005_auto_20200919_1736'),
]
operations = [
migrations.AlterModelOptions(
name='headerslider',
options={'verbose_name_plural': 'Head Slider'},
),
migrations.AlterModelOptions(
name='products',
options={'verbose_name_plural': 'Products'},
),
migrations.AddField(
model_name='products',
name='owner',
field=models.ForeignKey(default=django.contrib.auth.models.User, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
答案 0 :(得分:0)
确保已运行makemigration / migrate。 您的urls.py出了点问题,因为它正在等待标识符,并且似乎您传递了对象类型(我不确定)。我认为这里需要更多解释。 如果您仍未解决问题,请在此处提供更多详细信息。
答案 1 :(得分:0)
显然,您以某种方式弄乱了迁移文件。 我很确定问题出在以下语句中:
migrations.AddField(
model_name='products',
name='owner',
field=models.ForeignKey(default=django.contrib.auth.models.User, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
我认为根本不应该将default
参数传递给models.ForeignKey
(我相信这是导致错误TypeError: Field 'id' expected a number but got <User: >.
的原因),加上{{1 }}应该是模型文件中指定的name
,而不是'user'
。
这就是我认为这应该起作用的方式:
'owner'
如果按照我的建议进行更改,将无法解决该问题,
migrations.AddField(
model_name='products',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
./manage.py makemigrations