Object is not subscriptable in django

Question:

I have this function in django:

def get_customer(request):
    resp = dict(succees=False, message='no se encontro clientes')
    database = request.user.company_select.company_db
    try:
        customers = Customer.objects.using(database).get(id_customers=request.data['id_customer'])
        if customers:
            list_customers = list()
            customers['subgroup_p'] = Customer_Subgroup.objects.using(database).values().filter(id_subgroup=customers['customers_subgroup'])
            customers['group_p'] = Customer_Group.objects.using(database).values().filter(id_group=customers['subgroup_p']['group_id'])
            customers['customers_subgroup_p'] = Catalog_Type_Customer.objects.using(database).values().filter(id_type_customer=customers['customers_type'])
            customers['city_p'] = City.objects.values().filter(city_id=customers['city'])
            customers['state_p'] = State.objects.values().filter(state_id=customers['city_p']['state_id'])
            customers['country_p'] = Country.objects.values().filter(country_id=customers['state_p']['country_id'])
            list_customers.append(customers)
            resp.update(dict(success=True, message='', customers=list_customers))
    except Exception as e:
        print(e)
        resp.update(dict(message='Error'))

    return Response(resp)

But i get the error ´Customer´ is not subscriptable

What can i do to solve this?

Thanks!

Asked By: Luis Bermúdez

||

Answers:

You’re trying to access a django model object property as dict but you need to access it as a property using .property_name like customers.subgroup_p.

try this:

def get_customer(request):
    resp = dict(succees=False, message='no se encontro clientes')
    database = request.user.company_select.company_db
    try:
        customers = Customer.objects.using(database).get(id_customers=request.data['id_customer'])
        if customers:
            list_customers = list()
            customers.subgroup_p = Customer_Subgroup.objects.using(database).values().filter(id_subgroup=customers.customers_subgroup)
            customers.group_p = Customer_Group.objects.using(database).values().filter(id_group=customers.subgroup_p.group_id)
            customers.customers_subgroup_p = Catalog_Type_Customer.objects.using(database).values().filter(id_type_customer=customers.customers_type)
            customers.city_p = City.objects.values().filter(city_id=customers.city)
            customers.state_p = State.objects.values().filter(state_id=customers.city_p.state_id)
            customers.country_p = Country.objects.values().filter(country_id=customers.state_p.country_id)
            list_customers.append(customers)
            resp.update(dict(success=True, message='', customers=list_customers))
    except Exception as e:
        print(e)
        resp.update(dict(message='Error'))

    return Response(resp)
Answered By: Diego Magalhães
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.