在/ get返回的MultipleObjects返回了多个订单-返回了2

如何解决在/ get返回的MultipleObjects返回了多个订单-返回了2

我正在与denis ivy一起编码以建立一个电子商务网站,现在当我结帐订单时,如预期的那样完成了,但是当我返回首页时,出现此错误MultipleObjectsReturned / get()返回了多个订单-它返回了2个! 然后,一旦我删除了数据库中所有的订单,一切正常,直到我再次结帐,这意味着我遇到了问题,即我的数据库中没有两个订单,我真的不知道这有什么帮助非常感谢,我现在分享我的views.py

from django.shortcuts import render
from .models import *
from django.http import JsonResponse
import json
import datetime

def store(request):    
    if request.user.is_authenticated:
        customer = request.user.customer
        order,created = Order.objects.get_or_create(customer=customer)
        items = order.orderitem_set.all()
        cartItems = order.get_cart_items
    else:
        items = []
        order = {
            'get_cart_total':0,'get_cart_items': 0,'shipping': False
        }
        cartItems = order['get_cart_items']
    products = Product.objects.all()
    context= {'products': products,'cartItems': cartItems,}
    return render(request,'store/store.html',context)

def cart(request):
    if request.user.is_authenticated:
        customer = request.user.customer
        order,'shipping': False
        }
        cartItems = order['get_cart_items']

    context= {'items':items,'order':order,'cartItems': cartItems}
    return render(request,'store/cart.html',context)

def checkout(request):    
    if request.user.is_authenticated:
        customer = request.user.customer
        order,'store/checkout.html',context)

def updateItem(request):
    data = json.loads(request.body)
    productId = data['productId']
    action = data['action']
    print('Action:',action)
    print('Product:',productId)

    customer = request.user.customer
    product = Product.objects.get(id=productId)
    order,created = Order.objects.get_or_create(customer=customer,complete=False)

    #orderItem,created = OrderItem.objects.get_or_create(order=order,product=product)

    if action == 'add':
        orderItem,product=product)
        orderItem.quantity = (orderItem.quantity + 1)
    elif action == 'remove':
        orderItem = OrderItem.objects.filter(order=order,product=product)
        orderItem.quantity = (orderItem.quantity - 1)
        
    orderItem.save()

    if orderItem.quantity <= 0:
        orderItem.delete()

    return JsonResponse('Item was added',safe=False)

def processOrder(request):
    transaction_id = datetime.datetime.now().timestamp()
    data = json.loads(request.body)

    if request.user.is_authenticated:
        customer = request.user.customer
        order,complete=False)
        total = float(data['form']['total'])
        order.transaction_id = transaction_id

        if total == float(order.get_cart_total):
            order.complete = True 
        order.save()

        if order.shipping == True:
            ShippingAddress.objects.create(
                customer=customer,order=order,address=data['shipping']['address'],city=data['shipping']['city'],state=data['shipping']['state'],zipcode=data['shipping']['zipcode'],)

    else:
        print('User is not logged in ')

    return JsonResponse('Payment Complete!',safe=False)

我的模型。py

from django.db import models
from django.contrib.auth.models import User

class Customer(models.Model):
    user = models.OneToOneField(User,null=True,blank=True,on_delete=models.CASCADE)
    name = models.CharField(max_length=200,null=True)
    email = models.CharField(max_length=200,null=True)

    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10,decimal_places=2)
    digital = models.BooleanField(default=False,blank=True)
    image = models.ImageField(null=True,blank=True)

    def __str__(self):
        return self.name

    @property
    def imageURL(self):
        try:
            url = self.image.url
        except:
            url = ''
        return url

class Order(models.Model):
    customer = models.ForeignKey(Customer,on_delete=models.SET_NULL,blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100,null=True)

    def __str__(self):
        return str(self.id)

    @property 
    def get_cart_total(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.get_total for item in orderitems])
        return total

    @property 
    def get_cart_items(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.quantity for item in orderitems ])
        return total

    @property
    def shipping(self):
        shipping = False
        orderitems = self.orderitem_set.all()
        for i in orderitems:
            if i.product.digital == False:
                shipping = True
        return shipping

class OrderItem(models.Model):
    product = models.ForeignKey(Product,null=True)
    order = models.ForeignKey(Order,null=True)
    quantity = models.IntegerField(default=0,blank=True)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.product.name

    @property
    def get_total(self):
        total = self.product.price * self.quantity 
        return total

class ShippingAddress(models.Model):
    customer = models.ForeignKey(Customer,null=True)
    address = models.CharField(max_length=200,null=False)
    city = models.CharField(max_length=200,null=False)
    state = models.CharField(max_length=200,null=False)
    zipcode = models.CharField(max_length=200,null=False)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.address

我的checkout.html包含大量JavaScript的地方

{% extends 'store/main.html' %} {% load static %} {% block content %}
<div class="row">
    <div class="col-lg-6">
        <div class="box-element" id="form-wrapper">
            <form id="form">
                {% csrf_token %}
                <div id="user-info">
                    <div class="form-field">
                        <input required class="form-control" type="text" name="name" placeholder="Name..">
                    </div>
                    <div class="form-field">
                        <input required class="form-control" type="email" name="email" placeholder="Email..">
                    </div>
                </div>

                <div id="shipping-info">
                    <hr>
                    <p>Shipping Information:</p>
                    <hr>
                    <div class="form-field">
                        <input class="form-control" type="text" name="address" placeholder="Address..">
                    </div>
                    <div class="form-field">
                        <input class="form-control" type="text" name="city" placeholder="City..">
                    </div>
                    <div class="form-field">
                        <input class="form-control" type="text" name="state" placeholder="State..">
                    </div>
                    <div class="form-field">
                        <input class="form-control" type="text" name="zipcode" placeholder="Zip code..">
                    </div>
                    <div class="form-field">
                        <input class="form-control" type="text" name="country" placeholder="Zip code..">
                    </div>
                </div>

                <hr>
                <input id="form-button" class="btn btn-success btn-block" type="submit" value="Continue">
            </form>
        </div>

        <br>
        <div class="box-element hidden" id="payment-info">
            <small>Paypal Options</small>
            <button id="make-payment">Make Payment</button>
        </div>

    </div>

    <div class="col-lg-6">
        <div class="box-element">
            <a class="btn btn-outline-dark" href="{% url 'cart' %}">&#x2190; Back to Cart</a>
            <hr>
            <h3>Order Summary</h3>
            <hr> {% for item in items %}
            <div class="cart-row">
                <div style="flex:2"><img class="row-image" src="{{ item.product.imageURL }}"></div>
                <div style="flex:2">
                    <p>{{ item.product.name }}</p>
                </div>
                <div style="flex:1">
                    <p>{{ item.product.price|floatformat:2 }}</p>
                </div>
                <div style="flex:1">
                    <p>x{{item.quantity}}</p>
                </div>
            </div>
            {% endfor %}
            <h5>Items: {{order.get_cart_items}}</h5>
            <h5>Total: C{{order.get_cart_total|floatformat:2}}</h5>
        </div>
    </div>
</div>


<script type="text/javascript">
    var shipping = '{{ order.shipping }}'
    var total = '{{ order.get_cart_total }}'

    if (shipping == 'False') {
        document.getElementById('shipping-info').innerHTML = ''
    }

    if (user != 'AnonymousUser') {
        document.getElementById('user-info').innerHTML = ''
    }

    if (shipping == 'False' && user != 'AnonymousUser') {
        document.getElementById('form-wrapper').classList.add("hidden");
        document.getElementById('payment-info').classList.remove("hidden");
    }

    var form = document.getElementById('form')

    form.addEventListener('submit',function(e) {
        e.preventDefault()
        console.log('Form Submitted....')
        document.getElementById('form-button').classList.add("hidden");
        document.getElementById('payment-info').classList.remove("hidden");
    })

    document.getElementById('make-payment').addEventListener('click',function(e) {
        submitFormData()
    })

    function submitFormData() {
        console.log('Payment button clicked')

        var userFormData = {
            'name': null,'email': null,'total': total,}

        var shippingInfo = {
            'address': null,'city': null,'state': null,'zipcode': null,}

        if (shipping != 'False') {
            shippingInfo.address = form.address.value
            shippingInfo.city = form.city.value
            shippingInfo.state = form.state.value
            shippingInfo.zipcode = form.zipcode.value
        }

        if (user == 'AnonymousUser') {
            userFormData.name = form.name.value
            userFormData.email = form.email.value
        }

        var url = '/process_order/'

        fetch(url,{
                method: 'POST',headers: {
                    'Content-Type': 'application/json','X-CSRFToken': csrftoken,},body: JSON.stringify({
                    'form': userFormData,'shipping': shippingInfo
                })
            })
            .then((response) => response.json())
            .then((data) => {
                console.log('Success:',data);
                alert('Transaction completed');
                window.location.href = "{% url 'store' %}"
            })

    }
</script>

{% endblock content %}

我的cart.js也有很多JavaScript

var updateBtns = document.getElementsByClassName('update-cart')

for (i = 0; i < updateBtns.length; i++) {
    updateBtns[i].addEventListener('click',function() {
        var productId = this.dataset.product
        var action = this.dataset.action
        console.log('productId:',productId,'Action:',action)

        console.log('USER:',user)
        if (user == 'AnonymousUser') {
            console.log('User is not authenticated')
        } else {
            updateUserOrder(productId,action)
        }
    })
}

function updateUserOrder(productId,action) {
    console.log('User is authenticated,sending date...')

    var url = '/update_item/'

    fetch(url,{
            method: 'POST',headers: {
                'Content-Type': 'application/json',body: JSON.stringify({ 'productId': productId,'action': action })
        })
        .then((response) => {
            return response.json()
        })
        .then((data) => {
            location.reload()
        });
}

与cart.js相关的我的cart.html

{% extends 'store/main.html' %} {% load static %} {% block content %}
<div class="row">
    <div class="col-lg-12">
        <div class="box-element">

            <a class="btn btn-outline-dark" href="{% url 'store' %}">&#x2190; Continue Shopping</a>

            <br>
            <br>
            <table class="table">
                <tr>
                    <th>
                        <h5>Items: <strong>{{ order.get_cart_items }}</strong></h5>
                    </th>
                    <th>
                        <h5>Total:<strong>{{ order.get_cart_total|floatformat:2 }}</strong></h5>
                    </th>
                    <th>
                        <a style="float:right; margin:5px;" class="btn btn-success" href="{% url 'checkout' %}">Checkout</a>
                    </th>
                </tr>
            </table>

        </div>

        <br>
        <div class="box-element">
            <div class="cart-row">
                <div style="flex:2"></div>
                <div style="flex:2"><strong>Item</strong></div>
                <div style="flex:1"><strong>Price</strong></div>
                <div style="flex:1"><strong>Quantity</strong></div>
                <div style="flex:1"><strong>Total</strong></div>
            </div>
            {% for item in items %}
            <div class="cart-row">
                <div style="flex:2"><img class="row-image" src="{{ item.product.imageURL }}"></div>
                <div style="flex:2">
                    <p>{{ item.product.name }}</p>
                </div>
                <div style="flex:1">
                    <p>C{{ item.product.price|floatformat:2 }}</p>
                </div>
                <div style="flex:1">
                    <p class="quantity">{{ item.quantity }}</p>
                    <div class="quantity">
                        <img data-product="{{ item.product.id }}" data-action="add" class="chg-quantity update-cart" src="{% static  'images/arrow-up.png' %}">

                        <img data-product="{{ item.product.id }}" data-action="remove" class="chg-quantity update-cart" src="{% static  'images/arrow-down.png' %}">
                    </div>
                </div>
                <div style="flex:1">
                    <p>C{{ item.get_total }}</p>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock content %}

如果需要其他任何方法来解决该问题,请告诉我,我将其添加进来,我们将不胜感激!

解决方法

我解决了这个问题,解决方案是在我的视图中的以下语句中添加complete = False。

order,created = Order.objects.get_or_create(customer=customer,complete=False)
,

方法get_or_create检查是否存在一个且只有一个具有某些条件的对象,如果不存在,则会为您创建该对象。仅在确定在每种情况下数据库中仅存在该过滤器的一个或零个实例(即数据库中的唯一字段)时,才使用此方法。另请注意,如果您创建多个满足order,complete=False)的记录,则customer=customer,complete=False行可能会像将来一样引发异常。因此,请更改您的Order约束(即添加uniqueunique_together或什至是唯一字段的新字段)或将此部分更改为:

orders = Order.objects.filter(customer=customer,complete=False)

if not orders.exists():
    order = Order.objects.create(customer=customer,complete=False)
else:
    order = orders.last()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-