gpt4 book ai didi

python - 尝试从 django 中的 mysql 获取数据时出现值错误

转载 作者:行者123 更新时间:2023-11-30 21:45:34 25 4
gpt4 key购买 nike

我正在运行一个 django 应用程序并且我已经实现了 MySQL。当我运行服务器并尝试加载页面时,出现错误“十六进制 UUID 字符串格式错误”。这发生在 inituuid.py 文件的第 140 行,据我所知,发生这种情况是因为值 hex 不是长度 32 但我不知道如何解决这个问题。这是数据库中的条目所遵循的模型。

from django.db import models
import uuid
from django.db.models import (
UUIDField,
CharField,
TextField,
IntegerField,
DecimalField,
ImageField
)
# Create your models here.

class AnimeCatalog(models.Model):
anime_id = UUIDField(primary_key = True, default=uuid.uuid4, editable=False)
name = CharField(max_length=300)
genre = CharField(max_length=300)
typeanime = CharField(max_length = 10)
episodes = IntegerField(default=0)
rating = DecimalField(max_digits = 4, decimal_places = 2, null = True)
members = IntegerField()
anime_cover = ImageField(blank = True, null = True, upload_to = "img/animeCover", verbose_name = "Profile Photo")

然后我尝试在此模型上调用 objects.all() 方法以从下面的 views.py 文件中的 mysql 中检索它

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.views import View
from .forms import AnimeCatalogForm
from .models import AnimeCatalog
from .WebScraping import findAnimePic
import csv
from django.core.paginator import Paginator

# Create your views here.
def home(request):
temp = []
tempID = []
pics = []
fiveNames = []
names = []

new = AnimeCatalog.objects.all()

for line in new:
temp.append(line['name'].replace(''', ''))
tempID.append(line['anime_id'])

for x in range(0,5):
pics.append(findAnimePic(tempID[x],temp[x]))

for x in range(0, len(temp), 5):
for y in range (5):
if not(x+y >= len(temp)):
fiveNames.append({'name':temp[x+y],'img':pics[y]})
names.append(fiveNames)
fiveNames = []
items = {'names': names, 'imgs': pics}
html = render(request, 'home/home.html', {'names':names})
return html

这也是我导入 csv 文件的 mysql 中前几个条目的图片。 Image of mysql database with imported csv files

最佳答案

从该屏幕截图可以清楚地看出,anime_id 包含整数,而不是 UUID。我不知道你为什么将它设置为 UUIDField,但你应该改用 AutoField 并删除默认值。

关于python - 尝试从 django 中的 mysql 获取数据时出现值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49644252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com