我有2个ArrayField列表,我想使用zip方法将它们加入,因此我可以在Django我的Django模板的两个列表中进行迭代。但是输出将每个数组都视为单独的列表。
我的模特
class programas(models.Model):
title = models.CharField(unique=True, max_length=250)
script = ArrayField(models.CharField(max_length=8000, blank=True))
created_on = models.DateTimeField()
show_date = models.CharField(max_length=300, blank=True, null=True)
script_eng = ArrayField(models.CharField(max_length=8000, blank=True))
description = models.CharField(max_length=2000, blank=True, null=True)
description_eng = models.CharField(max_length=2000, blank=True, null=True)
url = models.CharField(max_length=200, blank=True, null=True)
show_id = models.IntegerField(primary_key=True)
id = models.IntegerField(null=True)
我尝试加入列表的视图
def pages(request, id):
obj = programas.objects.get(id=id)
script = programas.objects.values_list('script')
script_eng = programas.objects.values_list('script_eng')
zip_scripts = list(zip(script , script_eng))
context = {'title': obj.title,
'show_date': obj.show_date,
'script' : obj.script,
'script_eng': obj.script_eng,
'description': obj.description,
'description_eng': obj.description_eng,
'show_id':obj.show_id,
'url': obj.url,
'zip_scripts' : zip_scripts,
}
return render(request, 'rtves/pages.html', context)
我在模板页面上的代码
{% if script %}
{% for text1, text2 in zip_scripts %}
<p> {{ text1 }} </p>
<p> {{ text2 }} </p>
{% endfor %}
{% else %}
<p>Nothing here</p>
{% endif %}
我必须在数据库中进行更改吗?
答案 0 :(得分:1)
programas.objects.values_list('script')
为您提供了 all 数据库中programas对象的脚本字段的列表。因此,您实际上有一个列表列表。
您可能想要的只是您已经拥有的对象中的字段。因此,无需在那里使用values_list
。
obj = programas.objects.get(id=id)
script = obj.script
script_eng = obj.script_eng
或者,直接将它们传递到zip(注意,无论如何都不需要list
):
obj = programas.objects.get(id=id)
zip_scripts = zip(obj.script, obj.script_eng)
答案 1 :(得分:0)
从您的角度来看它“不起作用”的原因是因为当您将查询集转换为内置可迭代对象时,您不会得到一个可迭代值,但可迭代一个项目元组,例如>
Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.
at com.google.protobuf.InvalidProtocolBufferException.truncatedMessage(InvalidProtocolBufferException.java:84)
at com.google.protobuf.CodedInputStream$ArrayDecoder.readRawBytes(CodedInputStream.java:1253)
at com.google.protobuf.CodedInputStream$ArrayDecoder.readBytes(CodedInputStream.java:907)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:506)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFrom(UnknownFieldSet.java:483)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFrom(UnknownFieldSet.java:596)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFrom(UnknownFieldSet.java:274)
at com.google.protobuf.CodedInputStream$ArrayDecoder.readGroup(CodedInputStream.java:828)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:510)
at com.google.protobuf.GeneratedMessageV3.parseUnknownField(GeneratedMessageV3.java:298)
at com.jd.ai.algor.easydl.core.model.flow.strategy.impl.StudentInfoOuterClass$Student.<init>(StudentInfoOuterClass.java:98)
at com.jd.ai.algor.easydl.core.model.flow.strategy.impl.StudentInfoOuterClass$Student.<init>(StudentInfoOuterClass.java:44)
at com.jd.ai.algor.easydl.core.model.flow.strategy.impl.StudentInfoOuterClass$Student$1.parsePartialFrom(StudentInfoOuterClass.java:670)
at com.jd.ai.algor.easydl.core.model.flow.strategy.impl.StudentInfoOuterClass$Student$1.parsePartialFrom(StudentInfoOuterClass.java:664)
at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:158)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:191)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:203)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:208)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:48)
at com.jd.ai.algor.easydl.core.model.flow.strategy.impl.StudentInfoOuterClass$Student.parseFrom(StudentInfoOuterClass.java:291)
at com.jd.ai.algor.easydl.core.model.flow.strategy.impl.SettlementDatasetValidateStrategy.main(SettlementDatasetValidateStrategy.java:79)
当您压缩并列出时,它变得更加奇怪,但是您可以使用
[('bill',), ('while',), ('participant',), ('you',)]
在您的情况下,整个代码大致为
list(map(o.itemgetter(0), qs))