[Answer]-Django – remove field name and brackets

1👍

values returns list of dicts, so you can access value by list index and key name:

try:
    pk = Jobmst.objects.db_manager('database1').extra(where=['jobmst_alias=%s'],
        params=[alias]).values('jobmst_id')[0]['jobmst_id']
except IndexError:
    pk = None

You can use values_list that returns only list of specified fields values and to access the value you need only index:

try:
    pk = Jobmst.objects.db_manager('database1').extra(where=['jobmst_alias=%s'],
        params=[alias]).values_list('jobmst_id', flat=True)[0]
except IndexError:
    pk = None
👤ndpu

Leave a comment