尝试从django中的mysql获取数据的值错误

时间:2022-03-27 15:43:23

I'm running a django app and I've implemented MySQL. When I run the server and try to load the page I get the error "badly formed hexadecimal UUID string." This occurs in the uuid.py file in init at line 140 and from what I can tell, this occurs because the value hex is not of length 32 but I don't know how to address this issue. This is the model that the entries in the database follow.

我正在运行一个django应用程序,我已经实现了MySQL。当我运行服务器并尝试加载页面时,我收到错误“格式错误的十六进制UUID字符串”。这发生在第140行的init中的uuid.py文件中,据我所知,这是因为值十六进制的长度不是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")

Then I try to call the objects.all() method on this model to retrieve it from mysql in the views.py file which is below

然后我尝试调用此模型上的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

Also this is a picture of the first few entries in the mysql which i imported a csv file. Image of mysql database with imported csv files

这也是我导入csv文件的mysql中的前几个条目的图片。带有导入的csv文件的mysql数据库的图像

1 个解决方案

#1


0  

As is clear from that screenshot, anime_id contains integers, not UUIDs. I don't know why you set it to be a UUIDField but you should use AutoField instead and remove the default.

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

#1


0  

As is clear from that screenshot, anime_id contains integers, not UUIDs. I don't know why you set it to be a UUIDField but you should use AutoField instead and remove the default.

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