Api

26 views
Skip to first unread message

Tosin Ayoola

unread,
Feb 28, 2020, 2:20:39 AM2/28/20
to django...@googlegroups.com
Good morning guyz,  
I need help on this one,  it on writing api that will Shows the product count for each manufacturer as users add products real time. 
I have no idea how to solve this problem,  i will any suggestion 
Thanks 

Motaz Hejaze

unread,
Feb 28, 2020, 4:12:10 AM2/28/20
to django...@googlegroups.com
Anything real time will be done by sockets , which i think is not the solution you need..


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHLKn731GXi2A2i3-7DkVaPrD1Ta%2Bu%2B-ccjK7bA0uW4gdsHjJA%40mail.gmail.com.

Naveen Arora

unread,
Feb 28, 2020, 7:28:44 AM2/28/20
to Django users
Create an API which returns count, then use javascript to update the front end part.

Tosin Ayoola

unread,
Feb 29, 2020, 12:47:59 AM2/29/20
to django...@googlegroups.com
halo having 2 issues now
(1st) getting an integrity err
(2nd) i have a api code that return's count but still i'm not getting wat i expected
below is my codes and the err msg i'm getting

###APIView
class CartViewSet(viewsets.ModelViewSet):
    serializer_class = CartDetailSerializer
    queryset = CartList.objects.all()


    #@detail_route(methods=['post', 'put'])
    def add_item(request, self, pk=None):
        cart = self.get_object()
        try:
            product = Product.objects.get(
                pk = request.data['product_id']
            )
            quantity = int(request.data['quantity'])
        except Exception as e:
            print (e)
            return Response({'status': 'fail' })

        if product.quantity < 0 or product.quantity - quantity < 0:
            print('sorry check back leta')
            return Response({'status': 'fail'})

         existing_cart = CartList.objects.filter(product=product,       
            cart=cart).first()
        if existing_cart:
            existing_cart.quantity +=quantity
            existing_cart.save()
        else:
            new_cart = CartList(cart=cart, product=product,       
                  quantity=quantity)
            new_cart.save()
            product_list =      
        Product.Objects.filter(manufacturer=manufacturer).count()
        serializer = CartSerializer(cart)
        return Response(serializer.data)

Serializer

class CartDetailSerializer(serializers.ModelSerializer):
    customer = UserSerializer(read_only =True)
    items = serializers.StringRelatedField(many=True)

    class Meta:
        model = CartList
        fields = ('id', 'customer', 'items', 'created', 'updated')


class CartListSerializer(serializers.ModelSerializer):
    class Meta:
        model = CartList
        fields = ('product', 'quantity')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Screenshot from 2020-02-29 06-39-54.png

Naveen Arora

unread,
Feb 29, 2020, 11:51:36 AM2/29/20
to Django users
Can you share the traceback of error you are facing ?

Cheers,
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Tosin Ayoola

unread,
Feb 29, 2020, 12:55:59 PM2/29/20
to django...@googlegroups.com, naveena...@gmail.com
i have it attached as an attached file already 

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c8a99cd5-7e52-459f-9f17-8d9e5272c963%40googlegroups.com.
Screenshot from 2020-02-29 06-39-54.png
Reply all
Reply to author
Forward
0 new messages