Django原始查询不提供任何值

2024-09-22 20:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份调查问卷。我喜欢使用原始查询获取行的值,但是当我在HTML文件中循环时,它只给我第一个值,而其余的11个给我none,所以我对这些值的添加也给了我none。我不明白我做错了什么。我以前用同样的方法做过这个手术,效果很好

由于某些原因,我用html输入(不是Django表单)提出了12个问题,但它工作得很好,它将值发布到数据库中,在那里我有所有的正值和负值

型号.py

class Vezetoi(models.Model):

    def __str__(self):
        return str(self.user_name)

    user_name = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
    vezetoi_v01 = models.IntegerField(  null=True)
    vezetoi_v02 = models.IntegerField(  null=True)
    vezetoi_v03 = models.IntegerField(  null=True)
    vezetoi_v04 = models.IntegerField(  null=True)
    vezetoi_v05 = models.IntegerField(  null=True)
    vezetoi_v06 = models.IntegerField(  null=True)
    vezetoi_v07 = models.IntegerField(  null=True)
    vezetoi_v08 = models.IntegerField(  null=True)
    vezetoi_v09 = models.IntegerField(  null=True)
    vezetoi_v10 = models.IntegerField(  null=True)
    vezetoi_v11 = models.IntegerField(  null=True)
    vezetoi_v12 = models.IntegerField(  null=True)

forms.py

class VezetoiForm(forms.ModelForm):
    class Meta:
        model = Vezetoi
        fields = ['vezetoi_v01', 'vezetoi_v02', 'vezetoi_v03', 'vezetoi_v04', 'vezetoi_v05', 'vezetoi_v06', 'vezetoi_v07', 'vezetoi_v08', 'vezetoi_v09', 'vezetoi_v10', 'vezetoi_v11', 'vezetoi_v12' ]

views.py-我没有在这里复制一些在本例中无用的部分

def detail(request, item_id):
    item = Vezetoi.objects.get(pk=item_id)

    #VEZETŐI TESZT
    vezetoi_hatekonysag = Vezetoi.objects.raw('SELECT ID, vezetoi_v01 + vezetoi_v02 + vezetoi_v03 + vezetoi_v04 + vezetoi_v05 + vezetoi_v06 + vezetoi_v07 + vezetoi_v08 + vezetoi_v09 + vezetoi_v10 + vezetoi_v11 + vezetoi_v12 AS hatekonysag FROM stressz_vezetoi WHERE id= %s', [item_id])

    context = {
        'item': item,
        'vezetoi_hatekonysag': vezetoi_hatekonysag, 

    }

    return render(request, 'stressz/detail.html', context)

details.html

  {% for i in vezetoi_hatekonysag %}
  <td>{{ i.hatekonysag }}</td>  
  
  <p>{{ i.vezetoi_v01 }} {{ i.vezetoi_v02 }} {{ i.vezetoi_v03 }} {{ i.vezetoi_v04 }} {{ i.vezetoi_v05 }} {{ i.vezetoi_v06 }} {{ i.vezetoi_v07 }} {{ i.vezetoi_v08 }} {{ i.vezetoi_v09 }} {{ i.vezetoi_v10 }} {{ i.vezetoi_v11 }} {{ i.vezetoi_v12 }} </p>
  {% endfor %}

结果(查询词只是占位符)

enter image description here

提前非常感谢


Tags: truemodelsitemnullintegerfieldv02v01v03
1条回答
网友
1楼 · 发布于 2024-09-22 20:23:17

由于select子句根本不选择这些列,因此在模板中以None的形式获取这些值。进一步说,对于这样一个简单的目的,不需要原始查询,只需使用annotate即可:

from django.db.models import F


def detail(request, item_id):
    item = Vezetoi.objects.annotate(
        hatekonysag = F('vezetoi_v01') + F('vezetoi_v02') + F('vezetoi_v03') + ...
    ).get(pk=item_id)
    context = {
        'item': item,
    }
    return render(request, 'stressz/detail.html', context)

现在,您可以在模板中简单地编写:

<td>{{ item.hatekonysag }}</td>  
  
<p>{{ item.vezetoi_v01 }} {{ item.vezetoi_v02 }} {{ item.vezetoi_v03 }} {{ item.vezetoi_v04 }} {{ item.vezetoi_v05 }} {{ item.vezetoi_v06 }} {{ item.vezetoi_v07 }} {{ item.vezetoi_v08 }} {{ item.vezetoi_v09 }} {{ item.vezetoi_v10 }} {{ item.vezetoi_v11 }} {{ item.vezetoi_v12 }} </p>

Note: Your model schema is very repetitive and your fields have null=True? Perhaps it is better to have a separate model that stores these values and has a ForeignKey to `Vezetoi to make a one-to-many relation.

相关问题 更多 >