Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating a table(cartitem) with records from two tables namely itemdetails and tonerdetails.my cartitem table has a generic foreign key which has a generic relation to itemdetails and tonerdetails.

now if i add a record from itemdetails as the first record and then i add a second record from tonerdetails with an id of 10 its added succesfully.but when i add a third record which is an itemdetails record with an id of 10 its not being added.

similarly if i add a record from tonerdetails as the first record and then i add a second record from itemdetails with an id of 10 its added succesfully.but when i add a third record which is an tonerdetails record with an id of 10 its not being added.

what i have noticed is that the operation will only work if the first record is an itemdetails record and the second record is also an itemdetails record with id 10 and the third record can be tonerdetails record with id 10.

similar goes if the first record is an tonerdetails record and the second record is also an tonerdetails record with id 10 and the third record can be itemdetails record with id 10.

i dont know if the below code has messed up something in the checking part of already existing ids

What I have tried:

if 'view_tonerdetails' in urlObject :
    print("reached ltid if")
    detail = TonerDetails.objects.get(id=detailId)
    print("this is the detail id within the toner loop:",detail.id)
    detail_q=ContentType.objects.get_for_model(detail)
    print("this is the content type id for toners in if
    statement:",int(detail_q.id))
    cart, created = Cart.objects.get_or_create(customer=customer,
    complete=False)
    if detail.id in oid and int(detail_q.id) in ctid: #11
        messages.success(request, "Toner already in cart")
    else:
        p1 = CartItem.objects.create(object_id=detail.id,
        cart=cart,content_type_id=int(detail_q.id))
        p1.save()
elif 'view_items_details' in urlObject:
    print("reached liid elif")
    detail = ItemDetails.objects.get(id=detailId)
    print("this is the detail id within the item details
    loop:",detail.id)
    detail_q = ContentType.objects.get_for_model(detail)
    print("this is the content type id for items in elif
    statement:",int(detail_q.id))
    cart, created = Cart.objects.get_or_create(customer=customer,
    complete=False)
    if detail.id in oid and int(detail_q.id) in ctid: #9
        messages.success(request, "Item already in cart")
    else:
        p1 = CartItem.objects.create(object_id=detail.id, cart=cart,
        content_type_id=int(detail_q.id))
        p1.save()
return JsonResponse('Item was added', safe=False)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900