简体   繁体   中英

How to use django ORM to query database

I have a model called 'dboinv_product' and this model has a field called 'product_name'

I need to use Django ORM in my views.py file to pull ALL the product names. The goal is to put it into a list this way I can serialize it using JSON.

Does anyone know which Django command(s) can be used to produce this list?

Essentially, I need the equivalent of "SELECT ALL product_name FROM dbo.inv_product"

values_list is what you are looking for.

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. 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM