gpt4 book ai didi

mysql - 在 Django 中显示从模型管理器返回的数据

转载 作者:行者123 更新时间:2023-11-29 03:26:43 24 4
gpt4 key购买 nike

我将 MySQL 与 Django 一起使用,但无法看到从我的模型管理器中执行的查询返回的数据。

页面使用表格呈现,边框和分页正常,但是,表格中没有显示任何字段值。

我猜想在返回查询结果和以 html 格式呈现它之间需要一个步骤,但我很困惑。

对于上下文,我正在设置我的管理器,以便我可以执行比 Django 提供的查询更复杂的查询。

我遵循了一些使用带有相当简单查询的模型管理器的示例作为开始 - .. 我在本网站之外研究过的许多引用资料之一:https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers

在花了很多时间搜索之后,我相信这里有人可以提供帮助。提前致谢!!

这是模型管理器:

class ElectionsManager(models.Manager):
def is_active(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
SELECT *
FROM
newvoterproject.fullvh vh1
WHERE
vh1.city = 'Glocester' and
vh1.current_party = 'd'
group by
vh1.city,
vh1.street_name,
vh1.street_name_2,
vh1.street_number,
vh1.unit
;""")
result_list = cursor.fetchall()
return result_list

这里是模型的片段:

class Election(models.Model):
voter_id = models.CharField(primary_key=True, max_length=25)
last_name = models.CharField(max_length=50, blank=True, null=True)
first_name = models.CharField(max_length=50, blank=True, null=True)
middle_name = models.CharField(max_length=50, blank=True, null=True)
current_party = models.CharField(max_length=50, blank=True, null=True)
street_number = models.CharField(max_length=50, blank=True, null=True)
street_name = models.CharField(max_length=50, blank=True, null=True)
street_name_2 = models.CharField(max_length=50, blank=True, null=True)
unit = models.CharField(max_length=50, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=50, blank=True, null=True)
zip_code = models.CharField(max_length=50, blank=True, null=True)
zip_code_4 = models.CharField(max_length=50, blank=True, null=True)
precinct = models.CharField(max_length=50, blank=True, null=True)
status = models.CharField(max_length=50, blank=True, null=True)
objects = ElectionsManager() # model manager

class Meta:
managed = False
verbose_name = 'Election'
verbose_name_plural = 'Elections'
db_table = 'fullvh'

def __str__(self):
return '%s %s' % (self.first_name, self.last_name)

从 View 中调用模型管理器:

def vhistfun(request):
election_table = Election.objects.is_active()
paginator = Paginator(election_table , 25) # Show 25 contacts per page - may want to change this to READ 25 at a time...
page = request.GET.get('page')
try:
electpage = paginator.page(page)
except PageNotAnInteger:
electpage = paginator.page(1)
except EmptyPage:
electpage = paginator.page(paginator.num_pages)

context = {'electpage': electpage,
}
return render(request, 'elections/electable.html', context)

.. 以及处理结果的 html 片段

    {% for elect in electpage  %}
<tr id="voterrowclass" class="">
<td> {{ elect.first_name|lower|capfirst }} </td>
<td> {{ elect.last_name|lower|capfirst }} </td>
<td> {{ elect.current_party}} </td>
<td> {{ elect.street_number}} {{ elect.unit}} </td>
<td> {{ elect.street_name|lower|capfirst}} {{ elect.street_name_2|lower|capfirst}} </td>
<td> {{ elect.city|lower|capfirst}} </td>
</tr> <!-- # model data sent from view -->
{% endfor %}

最佳答案

您的自定义管理器方法未返回选举对象;它返回代表数据库行的元组。因此,您不能通过字段名称在模板中引用结果,就好像它们是对象一样。

真的,这不是经理的职责;您的管理器方法应始终返回查询集。

关于mysql - 在 Django 中显示从模型管理器返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027659/

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