[英]How to use django ORM to query database
I have a model called 'dboinv_product' and this model has a field called 'product_name'我有一个名为“dboinv_product”的 model,这个 model 有一个名为“product_name”的字段
I need to use Django ORM in my views.py file to pull ALL the product names.我需要在 views.py 文件中使用 Django ORM 来提取所有产品名称。 The goal is to put it into a list this way I can serialize it using JSON.
目标是以这种方式将其放入列表中,我可以使用 JSON 对其进行序列化。
Does anyone know which Django command(s) can be used to produce this list?有谁知道可以使用哪个 Django 命令来生成此列表?
Essentially, I need the equivalent of "SELECT ALL product_name FROM dbo.inv_product"本质上,我需要相当于“SELECT ALL product_name FROM dbo.inv_product”
values_list
is what you are looking for. values_list
是您正在寻找的。
value= dboinv_product.objects.values_list('product_name', flat=True)
list = list(value) # to convert to a list
# flat=True to get list rather than tuple inside of the ValuesListQuerySet.
The document about values_list
is here.关于
values_list
的文档在这里。 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.