无法访问 Django-Oscar 中的产品价格?
Can't access price of a Product in Django-Oscar?
正在尝试访问产品的价格,使用 Docs。但是获取属性错误。
>>> from oscar.apps.partner import strategy, prices
>>> from oscar.apps.catalogue.models import *
>>> product = Product.objects.get(pk=1)
>>> info = strategy.fetch_for_product(product)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'fetch_for_product'
要查看我做的所有策略属性
>>> dir(strategy)
>>> ['Base', 'D', 'Default', 'DeferredTax', 'FixedRateTax', 'NoTax', 'PurchaseInfo',
'Selector', 'StockRequired', 'Structured', 'UK', 'US', 'UseFirstStockRecord',
'__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'availability', 'namedtuple', 'prices']
所以fetch_for_product不在策略的属性中。现在我如何获取特定产品的价格?
你上面导入的是策略模块。您想要的是 object 策略。获取策略最简单的方法是向策略selector求一个:
from oscar.apps.partner.strategy import Selector
selector = Selector()
strategy = selector.strategy(request=..., user=...)
purchase_info = strategy.fetch_for_product(product=...)
price = purchase_info.price
选择器很有用,因为它允许您根据上下文(特定用户、来自特定国家/地区的请求等)使用不同的策略。在您自己的商店中,您将使用自己的实现覆盖 Selector
,默认情况下它将 return Default
strategy.
有关详细信息,请参阅 the docs。
您可以制作一个带有自定义字段的新序列化程序,以使用 SerializeMethodField 加载具有价格、主图像和标题的类别产品。
from rest_framework import serializers
from oscar.apps.partner.strategy import Selector
class ProductsSerializer(serializers.ModelSerializer):
price = serializers.SerializerMethodField()
availability = serializers.SerializerMethodField()
images = serializers.SerializerMethodField()
class Meta:
model = Product
fields= ('id', 'title','availability', 'images', 'price',)
def get_price(self, obj):
strategy = Selector().strategy()
price = vars(strategy.fetch_for_product(obj).price)
price['final'] = price['excl_tax'] + price['tax']
return price
def get_availability(self, obj):
strategy = Selector().strategy()
availability = vars(strategy.fetch_for_product(obj).availability)
try:
return availability['num_available']
except KeyError:
return -1
def get_images(self, obj):
try:
return ['https://127.0.0.1:8000/'+str(obj.primary_image().original)]
except AttributeError:
return []
正在尝试访问产品的价格,使用 Docs。但是获取属性错误。
>>> from oscar.apps.partner import strategy, prices
>>> from oscar.apps.catalogue.models import *
>>> product = Product.objects.get(pk=1)
>>> info = strategy.fetch_for_product(product)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'fetch_for_product'
要查看我做的所有策略属性
>>> dir(strategy)
>>> ['Base', 'D', 'Default', 'DeferredTax', 'FixedRateTax', 'NoTax', 'PurchaseInfo',
'Selector', 'StockRequired', 'Structured', 'UK', 'US', 'UseFirstStockRecord',
'__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'availability', 'namedtuple', 'prices']
所以fetch_for_product不在策略的属性中。现在我如何获取特定产品的价格?
你上面导入的是策略模块。您想要的是 object 策略。获取策略最简单的方法是向策略selector求一个:
from oscar.apps.partner.strategy import Selector
selector = Selector()
strategy = selector.strategy(request=..., user=...)
purchase_info = strategy.fetch_for_product(product=...)
price = purchase_info.price
选择器很有用,因为它允许您根据上下文(特定用户、来自特定国家/地区的请求等)使用不同的策略。在您自己的商店中,您将使用自己的实现覆盖 Selector
,默认情况下它将 return Default
strategy.
有关详细信息,请参阅 the docs。
您可以制作一个带有自定义字段的新序列化程序,以使用 SerializeMethodField 加载具有价格、主图像和标题的类别产品。
from rest_framework import serializers
from oscar.apps.partner.strategy import Selector
class ProductsSerializer(serializers.ModelSerializer):
price = serializers.SerializerMethodField()
availability = serializers.SerializerMethodField()
images = serializers.SerializerMethodField()
class Meta:
model = Product
fields= ('id', 'title','availability', 'images', 'price',)
def get_price(self, obj):
strategy = Selector().strategy()
price = vars(strategy.fetch_for_product(obj).price)
price['final'] = price['excl_tax'] + price['tax']
return price
def get_availability(self, obj):
strategy = Selector().strategy()
availability = vars(strategy.fetch_for_product(obj).availability)
try:
return availability['num_available']
except KeyError:
return -1
def get_images(self, obj):
try:
return ['https://127.0.0.1:8000/'+str(obj.primary_image().original)]
except AttributeError:
return []