manosim
manosim

Reputation: 3690

Django, get an attribute from a model

I'm trying to get the value of a field from a model. The thing is that I'm getting the id if I use filter(pk=university). But what it returns is [{'name': u'Icecream Chocolate'}]. Is it possible to get its name without the [{'name': u' ... }]?

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

Upvotes: 1

Views: 1216

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799440

From the next section down in the docs:

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

Upvotes: 1

alecxe
alecxe

Reputation: 474201

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)

Upvotes: 1

Related Questions