【问题标题】:I want to navigate to the details of a product but it displays <QuerySet [<Product: Tshirt>]> and does not show any information Django ORM Python我想导航到产品的详细信息,但它显示 <QuerySet [<Product: Tshirt>]> 并且不显示任何信息 Django ORM Python
【发布时间】:2020-04-12 07:21:18
【问题描述】:

当我点击提供的链接时,它必须显示与特定商店相关的产品,但我收到错误消息 Unsupported lookup 'product_name' for CharField or join on the field not allowed.

请就此提出建议

product_name 是 ProductDetails 的外键,shop 是 Products 的外键

这是模板

&lt;strong&gt;&lt;a href="{{partone}}"&gt;{{tut.product_description}}&lt;/a&gt;&lt;/strong&gt;

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

标签: python django orm


【解决方案1】:

我认为,从设计角度来看,这种观点并不好。因为它正在处理两种情况。一个用于产品,另一个用于产品详细信息。我首先建议为此使用两个视图或将两个功能组合在一起。以下是如何组合它们的示例:

products = Product.objects.filter(Q(shop_name__shop_slug=single_slug)|Q(productdetails__test_something=singleslug))
return render(
    request=request,
    template_name='products/shop_products.html',
    context={"products": products, "slug":single_slug}
)

# template

{% for product in products %}
   <strong><a href="{{product.get_absolute_url}}">{{product.product_description}}</a></strong>
    {% for details in product.productdetails.all %}
         {{ details.text_something }}
    {% endfor %}
{% empty %}
   {{ slug }} not found
{% endfor %}

我正在使用Q 进行复杂的查询。我还从reverse relationship 在Product 和ProductDetails 之间获取产品详细信息。此外,此实现建议在模型中实现get_absolute_url。

【讨论】:

  • ]> not found 我仍然得到这个作为输出可能是什么原因。 @ruddra
  • 你能分享你渲染模板的截图吗?
  • 对不起,我不认为你正在实施答案建议的方式。因为在答案中,模板中没有查询集的引用。
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
相关资源
最近更新 更多