首先,抱歉我的英语不好。 我在访问要在模板中显示的特定值时遇到问题。其实我不知道该怎么做。
我需要在模板中显示有关产品的信息,问题是产品的完整信息存储在3个不同的模型中,即Products,ProductStock,ProductPrice
我可以访问其中2个ProductStock,然后从那里访问Product。但是我也需要ProductPrice中的值。
这是模型
产品型号
class Product(models.Model):
# Relations
supplier = models.ForeignKey(
Supplier,
verbose_name=_('supplier'),
on_delete=CASCADE,
)
manufacturer = models.ForeignKey(
Manufacturer,
verbose_name=_('manufacturer'),
on_delete=CASCADE,
)
family = models.ForeignKey(
Family,
verbose_name=_('family'),
on_delete=CASCADE,
)
category = ChainedForeignKey(
Category,
chained_field='family',
chained_model_field='family',
auto_choose=True,
verbose_name=_('category'),
)
subcategory = ChainedForeignKey(
Subcategory,
chained_field='category',
chained_model_field='category',
auto_choose=True,
verbose_name=_('subcategory'),
)
condition = models.ForeignKey(
ProductCondition,
default=1,
verbose_name=_('condition'),
on_delete=CASCADE,
)
# Attributes - Mandatory
name = models.CharField(
max_length=63,
unique=True,
verbose_name=_('name'),
)
slug = models.SlugField(
max_length=63,
unique=True,
editable=False,
verbose_name=_('slug'),
)
product_code = models.CharField(
max_length=20,
verbose_name=_('product code'),
unique=True,
null=True,
blank=True,
)
...
...
ProductStock模型
class ProductStock(models.Model):
# Relations
product = models.ForeignKey(
Product,
related_name='product_stock',
verbose_name=_('product'),
on_delete=CASCADE,
)
warehouse = models.ForeignKey(
Warehouse,
default=1,
verbose_name=_('warehouse'),
on_delete=CASCADE,
)
# Attributes - Mandatory
quantity = models.IntegerField(
default=0,
verbose_name=_('quantity'),
)
ProductPrice模型
class ProductPrice(models.Model):
# Relations
product = models.ForeignKey(
Product,
verbose_name=_('product'),
on_delete=CASCADE,
related_name='product_price'
)
branch_office = models.ForeignKey(
BranchOffice,
default=1,
verbose_name=_('branch office'),
on_delete=CASCADE,
)
# Attributes - Mandatory
# Costo de compra del producto neto
net_purchase_price = models.DecimalField(
default=0.00,
max_digits=10,
decimal_places=2,
help_text=_('net purchase price (without taxs)'),
verbose_name=_('net purchase price'),
)
# IVA aplicable al producto
tax_rule = models.SmallIntegerField(
choices=TAX_RULES,
default=1,
verbose_name=_('tax rule'),
)
# Precio de compra con IVA,
# se asigna automáticamente al guardar el objeto
purchase_price = models.DecimalField(
default=0.00,
max_digits=10,
decimal_places=2,
editable=False,
verbose_name=_('purchase price'),
)
# Precio de venta al público
sell_price = models.DecimalField(
max_digits=10,
decimal_places=2,
verbose_name=_('sell price'),
)
# Precio de venta neto,
# se asigna automáticamente al guardar el objeto
net_sell_price = models.DecimalField(
default=0.00,
max_digits=10,
decimal_places=2,
editable=False,
verbose_name=_('net sell price'),
)
# IVA gravado en el producto en la compra,
# se asigna automáticamente al guardar el objeto
buy_product_tax = models.DecimalField(
default=0.00,
max_digits=7,
decimal_places=2,
editable=False,
verbose_name=_('buy product tax'),
)
# IVA gravado en el producto en la venta,
# se asigna automáticamente al guardar el objeto
sell_product_tax = models.DecimalField(
default=0.00,
max_digits=7,
decimal_places=2,
editable=False,
verbose_name=_('sell product tax'),
)
minimal_quantity = models.SmallIntegerField(
default=1,
verbose_name=_('minimal quantity'),
)
available = models.BooleanField(
default=True,
)
那么我需要在模板中显示特定分支机构的产品列表,该分支机构的库存以及该分支机构的产品价格,因为我根据要去的分支机构而有不同的价格出售。
然后在列表功能中,我有了这个,可以看到产品列表和库存
def product_list(request, subcategory_id=None):
branch_office = request.user.userprofile.branch_office
warehouse = Warehouse.objects.get(store=branch_office)
subcategory = None
subcategories = Subcategory.objects.all().order_by('family', 'name')
if "all" not in request.GET:
products = ProductStock.objects.filter(
product__product_price__available=True,
warehouse=warehouse,
)
elif request.GET.get('all') == "yes":
products = ProductStock.objects.filter(warehouse=warehouse)
if subcategory_id:
subcategory = get_object_or_404(Subcategory, id=subcategory_id)
products = products.filter(product__subcategory=subcategory)
if "name" in request.GET:
name = request.GET.get('name')
products = products.filter(product__name__icontains=name)
我将产品发送到模板,并使用products.product.name访问产品名称,并使用products.quantity显示库存(显然在for循环中)
我的问题是,如何显示我从用户个人资料中获得的特定branch_office的产品价格。并在列表中显示产品价格
感谢您的帮助
答案 0 :(得分:0)
我想不出一种从模板直接从ProductPrice
访问ProductStock
的方法。您可能需要在视图中做一些额外的工作:
您可以使用:
# products is a ProductStock in your case
for product in products:
product_prices = ProductPrice.objects.filter(product=product, branch_office=branch_office)
或反向关系:
# products is a ProductStock in your case
for product in products:
product_prices = product.productprice_set.filter(branch_office=branch_office)
要通过模板轻松访问,可以使用临时属性。可能不是最佳做法:
product.product_prices = ...