gpt4 book ai didi

python - 数据库和模型仍然存在 DJango 中出现编程错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:18:56 25 4
gpt4 key购买 nike

过去 3 天,我尝试使用 urls.py 和 views.py 在浏览器上获取模型数据,但失败得很惨,尽管我能够在 json 文件中获取数据库,但不能在浏览器上获取数据库。这是错误日志[ http://dpaste.com/2DQZX1Z][1]代码在这里

models.py

from __future__ import unicode_literals

from django.db import models


class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.CharField(max_length=8000, blank=True, null=True)


class Books(models.Model):
bid = models.IntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.CharField(max_length=8000, blank=True, null=True)


class Bookauth(models.Model):
bid = models.ForeignKey(Books, models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, models.DO_NOTHING, db_column='aid', blank=True, null=True)

views.py

from __future__ import unicode_literals

from django.shortcuts import render
import requests
from django.http import HttpResponse
from django.db import models
from .models import Books


import json
from django.core import serializers

def index(request):
return HttpResponse("Hello, world. You're at the polls index.")


def getObject(request):
all_books = Books.objects.all()
html = serializers.serialize('json', all_books)
return HttpResponse(html)
#data = serializers.serialize("json", Books.objects.all())
#return HttpResponse(data, mimetype='application/json')

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^non', views.index, name = 'index'),
url(r'^auth', views.author_search, name = 'AuthSearch'),
url(r'^book', views.getObject, name = 'Books')
]

最佳答案

将模型更改为此,然后 makemigrations 然后迁移,然后在浏览器中检查

from __future__ import unicode_literals

from django.db import models


class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.TextField( blank=True, null=True)


class Books(models.Model):
bid = models.IntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.TextField(blank=True, null=True)


class Bookauth(models.Model):
bid = models.ForeignKey(Books, on_delete=models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, on_delete=models.DO_NOTHING, db_column='aid', blank=True, null=True)

关于python - 数据库和模型仍然存在 DJango 中出现编程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48072001/

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