Swappable models

All Salesman models are swappable using Django’s private swappable API used in Auth package. This allows for full control of Salesman models and makes adding new fields or features easier. Swapped models must extend their “Base” versions to maintain original functionality.

Warning

This feature must be enabled at the start of the project and before initial app migration due to a limitation in Django’s swappable API – Read more

Tip

It is recommended to configure and setup all Salesman models as swappable even if it’s not neccesary at the begining. This will future proof your application in case you wish to add it later.

Note

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

Create models

First you need to define your models. Make sure to inherit from the Base original model. To swap all Order models:

# models.py
from salesman.orders.models import (
    BaseOrder,
    BaseOrderItem,
    BaseOrderNote,
    BaseOrderPayment,
)


class Order(BaseOrder):
    pass


class OrderItem(BaseOrderItem):
    pass


class OrderPayment(BaseOrderPayment):
    pass


class OrderNote(BaseOrderNote):
    pass

The same can be done for the Basket models as well:

# models.py
from salesman.basket.models import BaseBasket, BaseBasketItem


class Basket(BaseBasket):
    pass


class BasketItem(BaseBasketItem):
    pass

Register models

Then register your models in settings.py:

SALESMAN_BASKET_MODEL = 'shop.Basket'
SALESMAN_BASKET_ITEM_MODEL = 'shop.BasketItem'
SALESMAN_ORDER_MODEL = 'shop.Order'
SALESMAN_ORDER_ITEM_MODEL = 'shop.OrderItem'
SALESMAN_ORDER_PAYMENT_MODEL = 'shop.OrderPayment'
SALESMAN_ORDER_NOTE_MODEL = 'shop.OrderNote'

After that make sure to generate and run the migrations.

python manage.py makemigrations shop
python manage.py migrate

Referencing models

Instead of referring to Salemsman models directly, you should use the helper function:

from salesman.core.utils import get_salesman_model

BasketItem = get_salesman_model('BasketItem')

That’s it! You are now the owner of all Salesman models and are free to modify as seen fit.