You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Django电商平台预算功能开发协助:多商品总价匹配需求

Django电商平台预算商品组合查询功能实现方案

一、商品模型确认(若未定义)

假设你的商品模型包含分类与价格核心字段,示例代码如下:

# models.py
from django.db import models

class Product(models.Model):
    CATEGORY_CHOICES = [
        ('shirt', '衬衫'),
        ('bag', '包'),
        ('shoe', '鞋'),
    ]
    name = models.CharField(max_length=100)
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    # 可添加图片、描述等其他字段

    def __str__(self):
        return self.name

二、核心视图逻辑

处理用户预算输入,查询三类商品并生成符合总价要求的组合:

# views.py
from django.shortcuts import render
from .models import Product

def budget_combinations(request):
    combinations = []
    budget = None
    error = None

    if request.method == 'POST':
        budget_input = request.POST.get('budget', '')
        try:
            budget = float(budget_input)
            # 提前过滤单类商品价格不超预算的,减少无效循环
            shirts = Product.objects.filter(category='shirt', price__lte=budget)
            bags = Product.objects.filter(category='bag', price__lte=budget)
            shoes = Product.objects.filter(category='shoe', price__lte=budget)

            # 生成三类商品的所有组合,过滤总价符合要求的
            for shirt in shirts:
                for bag in bags:
                    for shoe in shoes:
                        total = shirt.price + bag.price + shoe.price
                        if total <= budget:
                            combinations.append({
                                'shirt': shirt,
                                'bag': bag,
                                'shoe': shoe,
                                'total': round(total, 2)
                            })
        except ValueError:
            error = "请输入有效的数字预算"

    return render(request, 'budget_combinations.html', {
        'combinations': combinations,
        'budget': budget,
        'error': error
    })

性能优化提示

如果三类商品数量较多,三重循环会拖慢响应速度,可做以下优化:

  • 对每类商品按价格升序排序,当单类商品价格累加后已超预算时,提前终止循环
  • 限制每类商品查询数量(例如只取价格最低的20个)
  • 大数据量场景下,改用数据库联查或异步任务处理组合生成

三、URL路由配置

添加对应页面的路由:

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    # 其他已有路由
    path('budget-combos/', views.budget_combinations, name='budget_combinations'),
]

四、前端模板实现(budget_combinations.html)

构建用户输入表单与结果展示页面:

<!DOCTYPE html>
<html>
<head>
    <title>预算商品组合查询</title>
    <style>
        .combo-item { margin: 10px 0; padding: 8px; border-bottom: 1px solid #eee; }
        .error { color: #dc3545; }
    </style>
</head>
<body>
    <h1>预算商品组合查询</h1>
    
    {% if error %}
        <p class="error">{{ error }}</p>
    {% endif %}

    <form method="post">
        {% csrf_token %}
        <label for="budget">预算金额:</label>
        <input type="number" id="budget" name="budget" step="0.01" required placeholder="例如:7000">
        <button type="submit">查询符合条件的组合</button>
    </form>

    {% if budget %}
        <h2>¥{{ budget }} 以内的商品组合</h2>
        {% if combinations %}
            <div class="combo-list">
                {% for combo in combinations %}
                    <div class="combo-item">
                        <span>衬衫:{{ combo.shirt.name }}(¥{{ combo.shirt.price }})</span>
                        <span> + 包:{{ combo.bag.name }}(¥{{ combo.bag.price }})</span>
                        <span> + 鞋:{{ combo.shoe.name }}(¥{{ combo.shoe.price }})</span>
                        <strong> → 总价:¥{{ combo.total }}</strong>
                    </div>
                {% endfor %}
            </div>
        {% else %}
            <p>未找到符合预算的商品组合</p>
        {% endif %}
    {% endif %}
</body>
</html>

五、可选扩展功能

  • 支持用户选择组合类型(例如允许仅两类商品或单类商品的组合)
  • 添加组合排序(按总价从低到高或从高到低)
  • 对重复组合进行去重处理(针对同款不同规格的商品)

内容的提问来源于stack exchange,提问作者Adebola Ajewole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 16:33:33