【发布时间】:2020-04-12 07:21:18
【问题描述】:
当我点击提供的链接时,它必须显示与特定商店相关的产品,但我收到错误消息 Unsupported lookup 'product_name' for CharField or join on the field not allowed.
请就此提出建议
product_name 是 ProductDetails 的外键,shop 是 Products 的外键
这是模板
<strong><a href="{{partone}}">{{tut.product_description}}</a></strong>
Views.py
# first check to see if the url is in categories.
categories = [c.shop_slug for c in Shop.objects.all()]
if single_slug in categories:
matching_series = Product.objects.filter(shop_name__shop_slug=single_slug)
series_urls = {}
for m in matching_series.all():
part_one = Product.objects.filter(product_name=m.product_name)
series_urls[m] = part_one
return render(request=request,
template_name='products/shop_products.html',
context={"product_name": matching_series, "part_ones": series_urls})
product_details = [t.test_something for t in ProductDetails.objects.all()]
if single_slug in product_details:
this_product = ProductDetails.objects.get(test_something = single_slug)
return render(request, 'products/product_details.html',{"product":this_product})
return HttpResponse(f"{single_slug} does not correspond")
from django.db import models
# Create your models here.
# For shop in the beginning
# class ShopType(models.Model):
# shop_type = models.CharField(max_length=50)
# shop_type_description = models.TextField()
# def __str__(self):
# return self.shop_type
class Shop(models.Model):
shop_name = models.CharField(max_length=50)
shop_location = models.CharField(max_length=100)
shop_opening_time = models.CharField(max_length=10)
shop_slug = models.CharField(max_length = 20)
# shop_type = models.ForeignKey(
# ShopType, default=1, on_delete=models.SET_DEFAULT)
shop_image = models.ImageField(upload_to='products', null=True, blank=True)
shop_owner = models.CharField(max_length=100)
shop_description = models.TextField()
shop_added_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.shop_name
class Product(models.Model):
product_name = models.CharField(max_length=100)
product_description = models.TextField()
product_image = models.ImageField(
upload_to='products', null=True, blank=True)
product_price = models.DecimalField(max_digits=10, decimal_places=2)
product_slug = models.CharField(max_length = 20)
product_added_date = models.DateTimeField(auto_now=True)
shop_name = models.ForeignKey(
Shop, default=1, on_delete=models.SET_DEFAULT)
def __str__(self):
return self.product_name
class ProductDetails(models.Model):
product_name = models.ForeignKey(
Product, default=1, on_delete=models.SET_DEFAULT)
test_something = models.CharField(max_length=20)
def __str__(self):
return self.test_something
当我点击商店时,它会显示相关产品。现在,当我点击产品时,我想显示相关的产品详细信息。
我还尝试将产品链接到其详细信息 product_details = [t.test_something for t in ProductDetails.objects.all()] if single_slug in product_details: this_product = ProductDetails.objects.get(test_something = single_slug) return render (request, 'products/product_details.html',{"product":this_product}) URL = {{tut.product_description}} 但它返回什么都没有。并在 url 中显示了一个查询集。你能帮忙吗
【问题讨论】:
-
我觉得你需要使用
matching_series = Product.objects.filter(shop_name__shop_slug=single_slug).all() -
它仍然不起作用返回相同
-
/%3CQuerySet%20[%3CProduct:%20Tshirt%3E]%3E HTTP/1.1" 200 50