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')
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)
From the next section down in the docs:
item_name = Icecream.objects.filter(pk=icecream_id).values_list('name')
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')
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)
From the next section down in the docs:
item_name = Icecream.objects.filter(pk=icecream_id).values_list('name')
0 commentaires:
Enregistrer un commentaire