简体   繁体   English

如何限制每个字段在Django queryset中返回的字段

[英]How to limit fields returned in django queryset per field

I have rows of products, that tend to share some information, such as the same images (an image may show multiple products), descriptions, manufacturer, etc. 我有一排产品,这些产品往往会共享一些信息,例如相同的图像(一个图像可能显示多个产品),说明,制造商等。

I would like to choose some fields to only show the information once. 我想选择一些字段,仅显示一次信息。 I can't use distinct for this (I don't think) as the information is not always identicle, such as with the description, but may be only subtly different enough that I don't want to repeat it. 我不能为此使用“不相同”(我不认为),因为信息并不总是相同,例如与描述相同,但可能只是微妙的不同,以至于我不想重复。

At the moment, I am using a for loop to display fields from each result, which ends up with something like this: 此刻,我正在使用for循环显示每个结果的字段,结果如下所示: 这个:

This is the code I am using in my view: 这是我认为的代码:

def collection_detail(request, name=None):
    template = loader.get_template('/webapps/mywebapp/furniture/main_page/templates/main_page/detail.html')
    products = product.objects.filter(id=name)
    cart_product_form = CartAddProductForm()
    context={'products': products,
    'cart_product_form': cart_product_form}
    return HttpResponse(template.render(context))

What would be the appropriate way to do this in a view, ignoring subsequent rows for some fields? 在视图中忽略某些字段的后续行,哪种方法合适?

edit: Example queryset results 编辑:示例查询集结果

collection_name|manufacturer|product_type|description|image_url
----------------------------------------------------------------
Testing        |FakeCo      |Bed         |pretty nice|/img/1.jpg
Testing        |FakeCo      |Desk        |pretty bad |/img/2.jpg
Testing        |FakeCo      |Nightstand  |pretty ok  |/img/1.jpg
Testing        |FakeCo      |Draws       |pretty nice|/img/3.jpg

In the example of the above data, I want to show the collection name and manufacturer just once, so the first result would do, each product type, the first description regardless of what it says, and each image that is distinct and not a duplicate. 在上述数据的示例中,我只想显示一次集合名称和制造商,因此第一个结果将是每种产品类型,第一个描述(不管说什么)以及每个图像都是唯一的而不是重复的。

By this 这样

products = product.objects.filter(id=name)

it will return a queryset, that means it consists of several products information. 它将返回一个查询集,这意味着它包含多个产品信息。 If you want to get only single objects information change the query like below: 如果只想获取单个对象信息,请更改查询,如下所示:

products = product.objects.filter(id=name).first()
or,
products = product.objects.filter(id=name)[:1]

Update If you want to display some fields one value then use this: 更新如果要显示某些字段一个值,请使用此值:

{{ value|first }}
products = product.objects.filter(id=name)
collection_name = []
manufacturer = []
product_type = []
description = []
image_url = []
for product in products:
    collection_name.append(product.collection_name)
    manufacturer.append(product.manufacturer)
    product_type.append(product.product_type)
    description.append(product.description)
    image_url.append(product.image_url)
collection_name = set(collection_name)
manufacturer = set(manufacturer)
product_type = set(product_type)
description = set(description)
image_url = set(image_url)

It is little long but it will solve your problem. 时间不长,但是可以解决您的问题。 "set" will make all common names once in the list. “ set”将使所有通用名称一次出现在列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM