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

时间:2018-04-04 06:31:09

标签: python mysql django django-models django-views

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

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

1 个答案:

答案 0 :(得分:0)

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