python - 无法在 Django 中创建对象
问题描述
我是 Django 新手,我正在努力根据我的应用程序中的模型创建和保存对象models.py
我正在使用的代码:
def search(request):
try:
if request.method == 'POST':
product = request.POST.get('product')
listed, not_listed = scrapeSellers(product)
search = Searches(product=product,
no_of_sellers=len(listed),
valid_search=1,
best_price=listed[0]['cost'],
worst_price=listed[-1]['cost'])
search.save()
print(product)
print(len(listed))
print(listed[0]['cost'])
print(listed[-1]['cost'])
return render(request, 'app/search.html', {
'listed' : listed,
'not_listed' : not_listed,
})
except:
return render(request, 'app/search.html', {})
我的models.py
样子是这样的:
class Searches(models.Model):
id = models.AutoField(primary_key=True)
product = models.CharField(max_length=8)
no_of_sellers = models.IntegerField()
valid_search = models.BooleanField()
best_price = models.FloatField(blank=True)
worst_price = models.FloatField(blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
此外,我之后search.save()
的打印语句没有执行,所以看起来对象创建失败,然后没有执行函数的其余部分。
我在控制台中没有收到任何错误消息,我的数据库中也没有发生任何事情。据我所见,我正在按照 Django Docs 示例中的说明进行操作:
p = Person(name="Fred Flintstone", shirt_size="L")
p.save()
我正在使用 MySQL 数据库、Python 3.6 和 Django 2.1.7。
对此的任何帮助将不胜感激!
解决方案
推荐阅读
- terraform - 展平复合地图的输出内容
- python - Excel列数据需要转换成双引号括起来的值列表,然后通过排除开始括号写入json文件
- php - 在 codeigniter 中加入超过 2 个数据库表错误:1066
- azure - 如何在通过 webhook 调用 azure 自动化 runnbook 的 azure 函数中获取响应
- linux - 使用linux命令列出给定小时内目录中的文件
- unix - 使用 SFTP 以二进制模式将大型机文件下载到 Unix
- haskell - 箭头 (->) 作为 haskell 数据构造函数
- javascript - 无法使用 javascript 从输入中计算面积
- c# - Unity我正在尝试制作一个布尔来触发步行和跑步动画,但它们重叠
- reactjs - 地图后再次重新渲染