Я работаю над проектом Оскар для Индии, и теперь я хочу применять налоги на продукты.
Я следовал документам по применению налогов на цены и наличие , раздвоил приложение для партнеров. Когда я указывал ставку = ('0,20'), он применял налог ко всем продуктам в размере 20%, теперь я хочу сделать его динамичным.
Итак, я просмотрел код для стратегии. FixedRateTax и попытался реализовать код для get_rate (), поскольку он вызывается для всех продуктов.
Как я хочу сделать его динамичным, исходя из категории продукта, я хочу применить налог на продукт, который есть в get_rate ().
Итак, я создал модель в core / models.py
class CategoryTax(models.Model):
category = models.OneToOneField(Category, on_delete=models.CASCADE, unique=True)
gst_tax = models.DecimalField(max_digits=11, decimal_places=4, null=True, default=0)
def __str__(self):
return "%s" % self.category
Здесь импорт модели категорий работает нормально, но когда я перехожу к стратегии.py и импортирую модели из ядра и других приложений, django выдает исключение.
Мой forked_apps / partner / strategy.py:
from decimal import Decimal as D
from oscar.apps.partner import strategy, prices
from django.conf import settings
from oscar.core.loading import get_model
from core.models import CategoryTax
Partner = get_model('partner', 'Partner')
Category = get_model('catalogue', 'Category')
Product = get_model('catalogue', 'Product')
ProductCategory = get_model('catalogue', 'ProductCategory')
class Selector(object):
"""
Custom selector to return a India-specific strategy that charges GST
"""
def strategy(self, request=None, user=None, **kwargs):
return IndiaStrategy()
class IncludingVAT(strategy.FixedRateTax):
"""
Price policy to charge VAT on the base price
"""
# We can simply override the tax rate on the core FixedRateTax. Note
# this is a simplification: in reality, you might want to store tax
# rates and the date ranges they apply in a database table. Your
# pricing policy could simply look up the appropriate rate.
# The Tax rate applied here is 3 % GST Now
rate = D(settings.SITE_GST)
def get_rate(self, product, stockrecord):
"""
This method serves as hook to be able to plug in support for varying tax rates
based on the product.
TODO: Needs tests.
"""
try:
product = Product.objects.get(id=product.id)
prod_cat = ProductCategory.objects.get(product=product)
cat = Category.objects.get(id=prod_cat.category_id)
cat_tax = CategoryTax.objects.get(category=cat)
print("The Cat Tax: ", cat_tax)
except Exception as e:
print(e)
return self.rate
class IndiaStrategy(strategy.UseFirstStockRecord, IncludingVAT,
strategy.StockRequired, strategy.Structured):
"""
Typical India strategy for physical goods.
- There's only one warehouse/partner so we use the first and only stockrecord
- Enforce stock level. Don't allow purchases when we don't have stock.
- Charge Indian GST on prices. Assume everything is standard-rated.
"""
Исключение, которое я получаю:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
, а также: ImportError: cannot import name 'CategoryTax'
Предложите мне любой возможный метод, который я могу использовать, чтобы сделать эту работу.
Я использую Django 2.1.3, Django Oscar 1.6.4
Полная трассировка стека ошибок:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f623e655400>
Traceback (most recent call last):
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 296, in get_model
return apps.get_model(app_label, model_name)
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/registry.py", line 195, in get_model
self.check_models_ready()
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/registry.py", line 137, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 296, in get_model
return apps.get_model(app_label, model_name)
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/registry.py", line 195, in get_model
self.check_models_ready()
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/registry.py", line 137, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/venvs/test/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/test/abhushan/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/venvs/test/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/home/venvs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/home/venvs/test/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/venvs/test/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/home/venvs/test/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/venvs/test/project/core/models.py", line 8, in <module>
Category = get_model('catalogue', 'Category')
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 308, in get_model
import_module('%s.%s' % (app_config.name, MODELS_MODULE_NAME))
File "/home/venvs/test/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/venvs/test/lib/python3.6/site-packages/oscar/apps/catalogue/models.py", line 4, in <module>
from oscar.apps.catalogue.abstract_models import * # noqa
File "/home/venvs/test/lib/python3.6/site-packages/oscar/apps/catalogue/abstract_models.py", line 35, in <module>
Selector = get_class('partner.strategy', 'Selector')
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 59, in get_class
return get_classes(module_label, [classname], module_prefix)[0]
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 69, in get_classes
return class_loader(module_label, classnames, module_prefix)
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 141, in default_class_loader
local_module = _import_module(local_module_label, classnames)
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 182, in _import_module
return __import__(module_label, fromlist=classnames)
File "/home/test/forked_apps/partner/strategy.py", line 16, in <module>
Category = get_model('catalogue', 'Category')
File "/home/venvs/test/lib/python3.6/site-packages/oscar/core/loading.py", line 311, in get_model
return apps.get_registered_model(app_label, model_name)
File "/home/venvs/test/lib/python3.6/site-packages/django/apps/registry.py", line 270, in get_registered_model
"Model '%s.%s' not registered." % (app_label, model_name))
LookupError: Model 'catalogue.Category' not registered.