275👍
Employees.objects.values_list('eng_name', flat=True)
That creates a flat list of all eng_name
s. If you want more than one field per row, you can’t do a flat list: this will create a list of tuples:
Employees.objects.values_list('eng_name', 'rank')
48👍
- [Django]-Altering one query parameter in a url (Django)
- [Django]-Django models avoid duplicates
- [Django]-Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?
21👍
We can select required fields over values.
Employee.objects.all().values('eng_name','rank')
- [Django]-What is reverse()?
- [Django]-What are the limitations of Django's ORM?
- [Django]-How to implement followers/following in Django
8👍
Daniel answer is right on the spot. If you want to query more than one field do this:
Employee.objects.values_list('eng_name','rank')
This will return list of tuples. You cannot use named=Ture when querying more than one field.
Moreover if you know that only one field exists with that info and you know the pk id then do this:
Employee.objects.values_list('eng_name','rank').get(pk=1)
- [Django]-What is a django.utils.functional.__proxy__ object and what it helps with?
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Running a specific test case in Django when your app has a tests directory
3👍
Oskar Persson’s answer is the best way to handle it because makes it easier to pass the data to the context and treat it normally from the template as we get the object instances (easily iterable to get props) instead of a plain value list.
After that you can just easily get the wanted prop:
for employee in employees:
print(employee.eng_name)
Or in the template:
{% for employee in employees %}
<p>{{ employee.eng_name }}</p>
{% endfor %}
- [Django]-What is the difference between cached_property in Django vs. Python's functools?
- [Django]-Django F() division – How to avoid rounding off
- [Django]-Aggregate() vs annotate() in Django
2👍
You can use values_list alongside filter like so;
active_emps_first_name = Employees.objects.filter(active=True).values_list('first_name',flat=True)
More details here
- [Django]-How to force application version on AWS Elastic Beanstalk
- [Django]-Get protocol + host name from URL
- [Django]-Querying django migrations table
2👍
Employees.objects.filter().only('eng_name')
This will give a QuerySet of all rows, but with only the specified fields. It does NOT give a list like the other answers above. It gives your the OBJECT INSTANCES. Watch out; it is objects.filter().only() NOT just objects.only()
It is similar to SQL Query:
SELECT eng_name FROM Employees;
- [Django]-Django – Circular model import issue
- [Django]-Best practice for Django project working directory structure
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Django connection to postgres by docker-compose
- [Django]-Django development IDE
1👍
You can also use list with field names
field_names = ['product_id', 'name', 'price', 'status']
results = ModelName.objects.all().values_list(*field_names)
# or for example
# results = ModelName.objects.all().values_list(*field_names, named=True)
- [Django]-How to obtain and/or save the queryset criteria to the DB?
- [Django]-Django – Render the <label> of a single form field
- [Django]-Django auto_now and auto_now_add