奥斯卡优惠应用程序覆盖好处功能冲突错误

时间:2019-05-25 10:57:38

标签: python django python-3.x e-commerce django-oscar

我使用Oscar 1.6.4版本。我想覆盖PercentageDiscountBenefit中的oscar.app.offer.benefits.py。但是发生冲突错误。如何覆盖此功能?

from oscar.core.loading import get_classes

CorePercentageDiscountBenefit, CoreAbsoluteDiscountBenefit = get_classes('offer.benefits', ['PercentageDiscountBenefit', 'AbsoluteDiscountBenefit'])


class PercentageDiscountBenefit(CorePercentageDiscountBenefit):

    def apply(self, basket, condition, offer, discount_amount=None,
              max_total_discount=None):

        print(
            "\n\n\n\n",
            "PRINT SOMETHING",
            "\n\n\n\n",
        )
        return super().apply(basket, condition, offer, discount_amount,
              max_total_discount)


class AbsoluteDiscountBenefit(CoreAbsoluteDiscountBenefit):

    def apply(self, basket, condition, offer, discount_amount=None,
              max_total_discount=None):

        print(
            "\n\n\n\n",
            "PRINT SOMETHING",
            "\n\n\n\n",
        )
        return super().apply(basket, condition, offer, discount_amount,
              max_total_discount)

错误日志

RuntimeError at /basket/

Conflicting 'percentagediscountbenefit' models in application 'offer': <class 'oscar.apps.offer.benefits.PercentageDiscountBenefit'> and <class 'oscar_apps.offer.benefits.PercentageDiscountBenefit'>.

Request Method:     GET
Request URL:    http://localhost:8000/basket/
Django Version:     1.11.9
Exception Type:     RuntimeError
Exception Value:    

Conflicting 'percentagediscountbenefit' models in application 'offer': <class 'oscar.apps.offer.benefits.PercentageDiscountBenefit'> and <class 'oscar_apps.offer.benefits.PercentageDiscountBenefit'>.

Exception Location:     /home/seyidov/PycharmProjects/Backend/embahome-backend/.venv/lib/python3.5/site-packages/django/apps/registry.py in register_model, line 224
Python Executable:  /home/seyidov/PycharmProjects/Backend/embahome-backend/.venv/bin/python
Python Version:     3.5.2
Python Path:    

['/home/seyidov/PycharmProjects/Backend/embahome-backend',
 '/home/seyidov/PycharmProjects/Backend/embahome-backend',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/home/seyidov/PycharmProjects/Backend/embahome-backend/.venv/lib/python3.5/site-packages',
 '/home/seyidov/Documents/Programs/pycharm-2018.2.4/helpers/pycharm_matplotlib_backend',
 '/home/seyidov/PycharmProjects/Backend/embahome-backend']

1 个答案:

答案 0 :(得分:1)

您不能以这种方式覆盖福利类别。如果要创建将不同逻辑应用于核心逻辑的自定义好处,则需要创建自己的类,该类是核心Benefit模型的代理:

from oscar.apps.offer import models

class CustomPercentageDiscountBenefit(models.Benefit):

    class Meta:
        proxy = True

    def apply(self, basket, condition, offer, discount_amount=None, max_total_discount=None):
        # Apply your benefit here

这在more detail in the documentation中有解释。