[Answered ]-Django, get an attribute from a model

1👍

One option is to use objects.get():

item_name = Icecream.objects.get(pk=icecream_id).name

Or, if you still want to use filter(), but don’t want to see dictionary with name key, use values_list() with flat=True:

item_name = Icecream.objects.get(pk=icecream_id).values_list('name', flat=True)
👤alecxe

1👍

From the next section down in the docs:

item_name = Icecream.objects.filter(pk=icecream_id).values_list('name')

Leave a comment