[英]How can I use the single field of the queryset?
I would like to pick the value of "titolo", for example, from the query set.例如,我想从查询集中选择“titolo”的值。 This is my code:这是我的代码:
class PostUserListView(generic.DetailView):
model=Autore
template_name=('blogv1/post_list.html')
def get_context_data(self, **kwargs):
context=super().get_context_data(**kwargs)
articolo=Articolo.objects.filter(autore__id=context['autore'].id)
The articolo is <QuerySet [<Articolo: Ingegneria>]> articolo 是 <QuerySet [<Articolo: Ingegneria>]>
Models.py Articolo has 3 fields: Titolo (charField),Autore(foreign key) and DataPubblicazione (date field) Models.py Articolo 有 3 个字段:Titolo(charField)、Autore(外键)和 DataPubblicazione(日期字段)
I'd like to take the value "titolo" from the articolo "ingegneria"我想从 articolo "ingegneria" 中获取值 "titolo"
all what you want, the values_list method of the queryset.所有你想要的,查询集的 values_list 方法。
in your case:在你的情况下:
articolo_pliral=Articolo.objects.filter(autore__id=context['autore'].id).values_list('titolo', flat=True)
single_articolo = articolo_pliral[0] if articolo_pliral.exist() else ''
PLease read more about Django.orm.请阅读更多关于 Django.orm 的信息。 The Answers on this question exists in documentation: https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values-list这个问题的答案存在于文档中: https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values-list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.