Adding productsΒΆ

To add products to the basket they need to be registered in SALESMAN_PRODUCT_TYPES dictionary setting with values formatted as 'app_name.Model': 'path.to.ModelSerializer'.

Note

For this example, we assume your custom app is named shop.

1. Create product model

First, create a product model. Requirements are that it implements get_price(self, request) method and has properties name and code. Eg:

# models.py
from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=18, decimal_places=2)

    def get_price(self, request):
        return self.price

    @property
    def code(self):
        return str(self.pk)

2. Create product serializer

Then create a serializer for the product. Eg:

# serializers.py
from rest_framework import serializers

from .models import Product


class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ['name', 'code']

3. Register the product

The only thing left is to register it in settings.py:

SALESMAN_PRODUCT_TYPES = {
    'shop.Product': 'shop.serializers.ProductSerializer',
}

You can now add the product to the basket by sending a POST /basket/ request. In your request, you should include product_type with a value shop.Product and product_id with product instance id as the value.