【问题标题】:Forking shipping app has no effect in Django oscar分叉运输应用程序在 Django oscar 中无效
【发布时间】:2018-07-10 12:26:40
【问题描述】:

我已经使用 oscar_fork_app 命令将我的运输应用程序分叉到一个名为 forked_apps 的文件夹中,并在 settings.py get_core_apps(['forked_apps.shipping']) 中添加,我只想创建提到的两种运输方式,标准和快递此链接中的文档:https://django-oscar.readthedocs.io/en/latest/howto/how_to_configure_shipping.html.

init.py 我有这个代码预先存在:

default_app_config = 'forked_apps.shipping.config.ShippingConfig'

在repository.py中我是这样写的:

from oscar.apps.shipping import repository
from .methods import *


class Repository(repository.Repository):

    def get_available_shipping_methods(
            self, basket, user=None, shipping_addr=None,
            request=None, **kwargs):
        methods = (Standard())
        print("\n\nFetch availble shipping methods")
        if shipping_addr:
            # Express is only available in the UK
            methods = (Standard(), Express())

        return methods

在我写的methods.py中:

from decimal import Decimal as D
from oscar.apps.shipping import methods
from oscar.core import prices

class Standard(methods.FixedPrice):
    code = 'standard'
    name = 'Standard shipping'
    charge_excl_tax = D('5.00')

class Express(methods.FixedPrice):
    code = 'express'
    name = 'Express shipping'
    charge_excl_tax = D('10.00')

应该发生的是,shipping_methods.html 页面应该显示出来,但是在输入送货地址后,它会直接进入付款详情页面;这通常只有在没有定义运输方式的情况下才会发生,但是我在上面的代码中实现了两种运输方式,标准和快递。我不知道如何使它工作,即使打印语句不起作用。 还有其他我必须编写的附加代码吗?

如果你已经实现了,有人可以提供一些代码的解决方案吗?

【问题讨论】:

  • 我看不出您发布的代码有什么问题。您确定分叉的应用程序实际上正在加载(您的分叉的Repository 类上的__init__() 是否运行?)并且您正在测试的篮子需要运输(如果篮子中没有产品,则此代码不会运行) requires_shipping 在他们的产品类上设置为 true)?
  • 我认为methods = (Standard()) 应该是methods = (Standard(),),否则你返回的是错误的东西。在任何情况下,print 语句都应该被执行,如果不是,那么正如 solarissmoke 指出的那样,还有其他一些问题。 config.ShippingConfig 代码是否完好无损?
  • 好的,所以我在我的shell中做了这个>>> from forked_apps.shipping.repository import Repository >>> r=Repository() ,这之后没有错误。这是检查是否的方法init() 工作正常吗?
  • 不,这并不能告诉您 Oscar 是否正在加载您的课程。您需要使用get_class() 加载类,然后检查返回的是您的类而不是 Oscar 的默认值。
  • 在 settings.py 中,你需要在分叉后用这个 'apps.shipping.apps.ShippingConfig' 替换 'oscar.apps.shipping.apps.ShippingConfig'

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


【解决方案1】:

从设置中删除 oscar 应用。

例如:

#oscar.apps.checkout
#oscar.apps.shipping

等等

【讨论】:

  • 请推荐 Django-Oscar 初学者教程,文档除外,我可以在其中获得有关使用它和自定义模型以及已经存在的 Oscar 应用程序和模板的分步指南和说明?
【解决方案2】:

这部分给了我错误。我无法解决这个问题。

get_available_shipping_methods(
    self, basket, user=None, shipping_addr=None,
    request=None, **kwargs):
...

Django 版本。 > 2.1 |奥斯卡版> 最新

我是这样用的;

mkdir customapp
touch customapp/__init__.py

python manage.py oscar_fork_app shipping customapp/

编辑 settings.py

from oscar import get_core_apps
INSTALLED_APPS = INSTALLED_APPS + get_core_apps(
      ['customapp.shipping'])

在我们的 customapp/shipping 目录中添加了新文件,名为 (repository.py)

from oscar.apps.shipping import repository
from . import methods

class Repository(repository.Repository):

    methods = (methods.Standard(),)

然后在同一目录中添加新文件,customapp/shipping,称为(methods.py)

from oscar.apps.shipping import methods
from oscar.core import prices
from decimal import Decimal as D

class Standard(methods.Base):
    code = 'standard'
    name = 'Shipping (Standard)'

    def calculate(self, basket):
        return prices.Price(
            currency=basket.currency,
            excl_tax=D('5.00'), incl_tax=D('5.00'))

您可以添加更多方法。

然后运行这些命令;

python manage.py makemigrations
python manage.py migrate
python manage.py runserver

希望这会有所帮助。

【讨论】:

  • 嗨,我很久以前就想通了,谢谢你的帮助:)
猜你喜欢
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 2022-01-13
  • 1970-01-01
相关资源
最近更新 更多