l0n3_w01f
l0n3_w01f

Reputation: 85

Database and Models present still programming error is coming in DJango

I am trying to get the models data on the browser using the urls.py and views.py for last 3 days and failing miserably, although I am able to get the database in a json file but not on browser. This is the error log [http://dpaste.com/2DQZX1Z][1] and the code is here

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')
]

Upvotes: 1

Views: 48

Answers (1)

Exprator
Exprator

Reputation: 27543

change your models to this and then makemigrations then migrate and then check in the browser

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)

Upvotes: 1

Related Questions