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

标签 python mysql django django-models django-views

我正在运行一个 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/

相关文章:

python - 我如何确定我的数据库的连接限制应该是多少?

mysql - SQL:分组计数?

python - 我编写了一个重力模拟程序,可以让 2 个大小相同的球体聚集在一起,但是

python - 通过 getattr 访问方法

mysql - 将查询的 where 子句中的选择查询更改为查询中只有一个选择

django - 如何在 Django 中定义模型之间的多态关系?

django - 在Django Admin中链接到外键对象

python - 如何将 Ajax 与 Django 应用程序集成?

python - 在 Python 中休眠最有效的方法是什么?

php - 如何在 PHP mysql 中进行重复验证?