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 models
First, create a product model. It must implement the salesman.core.typing.Product
protocol.
Required methods and attributes are: id
, name
, code
, get_price(self, request)
.
# models.py
from django.db import models
class Product(models.Model):
"""
Simple single type product.
"""
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=18, decimal_places=2, default=0)
def __str__(self):
return self.name
def get_price(self, request):
return self.price
@property
def code(self):
return str(self.id)
2. Create product serializers
Then create a serializer for the product. Eg:
# serializers.py
from rest_framework import serializers
from . import models
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = models.Product
fields = ["name", "code"]
3. Register the product types
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
set as shop.Product
and
product_id
with product instance id as the value.