You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Django Oscar 1.6.1后台为单个商品添加推荐商品并设置排序

Hey there! Let's walk through how to populate the recommended products field for individual items in your Django Oscar 1.6.1 admin, plus set up sorting for them.

First, check if your product edit page already has a section for related/recommended products. If not, you'll need to add an inline to the Product admin. Here's how:

  1. In your project's admin.py (likely in a catalogue app override), import the necessary models and Oscar's default Product admin:
from django.contrib import admin
from oscar.core.loading import get_model
from oscar.apps.catalogue.admin import ProductAdmin as OscarProductAdmin

Product = get_model('catalogue', 'Product')
RelatedProduct = get_model('catalogue', 'RelatedProduct')
  1. Define an inline class for RelatedProduct:
class RelatedProductInline(admin.TabularInline):
    model = RelatedProduct
    fk_name = 'product'  # Links to the parent product being edited
    extra = 1  # Shows one empty row to add new recommended products
    raw_id_fields = ('recommended',)  # Uses an ID picker instead of a huge dropdown (better for large catalogs)
  1. Override the default ProductAdmin to include this inline:
class CustomProductAdmin(OscarProductAdmin):
    # Add our inline to the existing set of inlines from Oscar
    inlines = OscarProductAdmin.inlines + [RelatedProductInline]

# Unregister Oscar's default ProductAdmin and register our custom one
admin.site.unregister(Product)
admin.site.register(Product, CustomProductAdmin)

Restart your server, and when you edit a product in the admin, you'll now see a "Related Products" section at the bottom of the page.

Now that the admin section is set up:

  • Open the product you want to add recommendations for in the admin.
  • Scroll to the "Related Products" inline section.
  • Click "Add another Related Product".
  • Use the "Recommended" field to select the product you want to recommend (the raw ID picker lets you search by product ID or title).
  • Set the Display order field: this controls the sort order. Lower numbers appear first (e.g., set your top recommendation to 1, the next to 2, etc.).
  • Save the product when you're done adding all recommended items.

Step 3: Display Sorted Recommendations in Your Templates

To show the sorted recommended products on your frontend, fetch them ordered by the display_order field in your view:

from django.shortcuts import get_object_or_404
from oscar.core.loading import get_model

Product = get_model('catalogue', 'Product')

def product_detail(request, product_id):
    product = get_object_or_404(Product, pk=product_id)
    # Get recommended products sorted by display_order (ascending)
    recommended_products = product.related_products.order_by('display_order')
    return render(request, 'catalogue/product_detail.html', {
        'product': product,
        'recommended_products': recommended_products,
    })

Then in your template, loop through them:

{% if recommended_products %}
    <h3>Recommended Products</h3>
    <div class="recommended-products-grid">
        {% for item in recommended_products %}
            <div class="product-card">
                <a href="{{ item.get_absolute_url }}">
                    <img src="{{ item.primary_image.original.url }}" alt="{{ item.title }}">
                    <p>{{ item.title }}</p>
                    <span>{{ item.price }}</span>
                </a>
            </div>
        {% endfor %}
    </div>
{% endif %}

That's it! You should now be able to add recommended products in the admin and control their display order easily.

内容的提问来源于stack exchange,提问作者Sammy J

火山引擎 最新活动