简体   繁体   中英

CART functionallity in DRF

I am new to Django and currently setting up an ecommerce backend. I have created models, serializers, views and urls.

Now, I need to set up a cart functionality, like create/update/delete/calculate_total and etc.

The thing is I am totally lost. Don't know where to start and what to do next. If you have a little time for quick guidance I'd be sincerely grateful.

class Cart(models.Model):
    owner = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    number_of_items = models.PositiveIntegerField(default=0)
    total = models.DecimalField(default=0.00, max_digits=5, decimal_places=2)


    def __str__(self):
        return "User: {}, items in cart: {}".format(self.owner, self.number_of_items)



class CartItem(models.Model):
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE, default=None)
    item = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()

    def __str__(self):
        return str(self.id)



class CartSerializer(serializers.ModelSerializer):
    owner = serializers.StringRelatedField()
    class Meta:
        model = Cart
        fields = '__all__'


class CartItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = CartItem
        fields = "__all__"



class CartView(viewsets.ModelViewSet):
    serializer_class = CartSerializer
    queryset = Cart.objects.filter()

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

class CartItemView(viewsets.ModelViewSet):
    serializer_class = CartItemSerializer
    queryset = CartItem.objects.all()



router = DefaultRouter()

router.register(r'view', CartView)
router.register(r'item', CartItemView)

urlpatterns = [
    path('', include(router.urls)),
]

If I understand correctly you don't know where to move with your project and what feautures to add. If thats the case then I recommend adding things like: viewed items, wishlist, different categories and sub-categories, recommended items, some sort of test payment method, search with multiple paramaters, questions for items, messages between users and staff, a chat bot, item reviews etc.

My comment is too long for a comment so I post it here:

If you are just starting out with webdev/django I would start with CRUD functions to each of your models just to get to know everything and then move on to your needs after that.

I believe that will simplify what you have to wrap your head around. And you might get some ideas for what you want to do while setting up the CRUD functionality.

Add your models to your admin site so you can easily add some test data. Then you can start with making Generic display views to show that data, and then move on to Generic editing views and forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM