【发布时间】:2021-11-07 03:22:10
【问题描述】:
我一直在使用现有的数据表,因此我使用inspectdb 创建模型。我的班级没有主键,所以当我makemigrations 和migrate 时,Django 添加了一个名为id 的自动主键。后来我定义了模板、视图和url,在我的网站上查看类表时,出现这样的错误:
没有这样的列:AGC.id
我不知道如何解决它,我是使用 Django 的新手。
型号:
class Agc(models.Model):
index = models.BigIntegerField(blank=True, null=True)
area = models.TextField(db_column='AREA', blank=True, null=True) # Field name made lowercase.
periodo = models.BigIntegerField(db_column='PERIODO', blank=True, null=True) # Field name made lowercase.
demanda = models.TextField(db_column='DEMANDA', blank=True, null=True) # Field name made lowercase. This field type is a guess.
class Meta:
db_table = 'AGC'
模板:
{% extends "despachowebapp/Base.html" %}
{% load static %}
{% block content %}
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Index</th>
<th scope="col">Area</th>
<th scope="col">Periodo</th>
<th scope="col">Demanda</th>
</tr>
</thead>
<tbody>
{% if AGCs %}
{% for AGC in AGCs %}
<tr>
<th scope='row'>{{ Agc.id }}</th>
<td>{{ Agc.index }}</td>
<td>{{ Agc.index }}</td>
<td>{{ Agc.area }}</td>
<td>{{ Agc.periodo }}</td>
<td>{{ Agc.demanda }}</td>
</tr>
{% endfor %}
{% else %}
<h1>No hay datos </h1>
</tbody>
</table>
{% endblock %}
观看次数:
def index(request):
AGCs=Agc.objects.all()
contexto={'AGCs': AGCs }
return render(request,'OfertasAGC.html', contexto)
网址:
urlpatterns =[
path('admin/', admin.site.urls),
path('', include('webapp.urls')),
path('prueba/',views.index),
]
【问题讨论】:
-
请发布完整的回溯。
标签: django sqlite django-models operationalerror