如何在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.
Step 1: Ensure Recommended Products Are Visible in the Admin
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:
- 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')
- 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)
- 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.
Step 2: Add Recommended Products & Set Sort Order
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 to2, 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




