Home > Enterprise >  How to access value in QuerySet Django
How to access value in QuerySet Django

Time:11-23

I am creating a simple Pizza Delivery website and trying to add an option to choose a topping. When I want to print Ingredients it returns QuerySet and Values in it separated by a comma. Is there any option how can I get values based on their variable names (ex. ingredients.all[0].toppingName -> cheese) or is there any methods which allows to get values separately. Of course, kludge with .split works but it is awful

enter image description here Models

UPD:

As for admin panel the provided solution worked. But it does not work in html template, so I found this code. Maybe it will useful for somebody.

enter image description here

CodePudding user response:

In Django, if you want to get only a specific column value you can use

Model.objects.all().values('column_name')

You can also filter the queryset and get values as

Model.objects.filter(condition).values('column_name')
  • Related