How to add an own order numbers generator¶
LFS order numbers generator is pluggable. In this tutorial you will learn how to add an own one.
Please see also the
complete example application or refer to the default implementation
of LFS within
Create an application¶
First you need to create a default Django application (or use an existing one). If you do not know how to do this, please refer to the excellent Django tutorial.
The structure of the application should look at least like this:
my_order_numbers __init__.py models.py
Implement the application¶
Add the model¶
models.py file of your application create a class called
OrderNumberGenerator which inherits from LFS’ OrderNumberGenerator base
class and add a method to it called
from lfs.plugins import OrderNumberGenerator as Base class OrderNumberGenerator(Base): def get_next(self, formatted=True): return "DOE-4711"
get_next method is called when the shop customer submits a new order. It
must return a character value which will become the order number of the new
Plug in your order number generator¶
Now as the code is ready, you can easily plugin your payment method:
Add your application to the PYTHONPATH.
Add your class to
LFS_ORDER_NUMBER_GENERATOR = “my_order_numbers.models.OrderNumberGenerator”
Add your application to settings.INSTALLED_APPS and sync the database.
And that’s it¶
You should now see your form within the
Order Numbers tab within
Shop/Preference and the
get_next method of your model should be
called to generate a new order number.
Optionally Add your own Template¶
Optionally you can add your own HTML for the management interface. For this just add the order_numbers_tab.html template to your application:
my_order_numbers templates manage order_numbers order_numbers_tab.html
Please refer to the standard template of LFS to get more details. You can find this on following place:
In this case please make sure that your
that LFS’ default
order_numbers_tab.html template is overwritten.
- The form is automatically created from the model you provide. However you can
provide a own own by overwriting the
get_formmethod. See the API for more.
- If you just want to exclude certain fields from the automatically generated
form you can overwrite the
exclude_form_fields. The return value is just passed to the exclude attribute of the from Meta class. See the API for more.
get_next method of your new class you have access to following
- The current request
- The current user
- The current customer
- The current cart
- The order which is about to be created.
Please note that you have also access to the products of the order via the
items attribute. For instance:
for item in self.order.items.all(): product = item.product
See the also the
OrderItem classes for more information.