Django:get()返回了多个项目-返回了3 [英] Django: get() returned more than one items -- it returned 3

查看:46
本文介绍了Django:get()返回了多个项目-返回了3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误

MultipleObjectsReturned: get() returned more than one items -- it returned 3!.

我要编辑和更新数据库中的现有记录.下面是我的模型,视图和html代码.

I want edit and update an existing record in the database. Below are my model, views and html code.

Model.py

import datetime
from django.db import models
from django.utils import timezone

class Purchases(models.Model):
    bottle = models.CharField(max_length=20)
    bottle_purchased = models.CharField(max_length=20)
    date_added = models.DateTimeField(auto_now_add=True)
    bottle_total= models.CharField(max_length=20)
    transportation_cost = models.CharField(max_length=20)
    total_cost = models.CharField(max_length=20)

        class Meta:
            verbose_name_plural = 'Purchases'

        def __str__(self):
            return self.bottle

用于编辑的视图功能.Views.py

Views function for editing. Views.py

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, Http404
from django.urls import reverse

from .models import Purchases
from .forms import PurchasesForm


def purchases(request):
    purchases = Purchases.objects.order_by('date_added')
    context = {'purchases': purchases}
    return render(request, 'ht/purchases.html', context)

def edit_purchase(request):
    entry = get_object_or_404(Purchases)
    purchase = entry.purchase

    if request.method != 'POST':
        # Initial request; pre-fill form with the current entry
        form = PurchasesForm(instance=entry)
    else:
        # POST data submitted; process data.
        form = PurchasesForm(instance=entry, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('geo_gas:purchases'))

    context = {'entry': entry, 'purchases': purchases, 'form': form}
    return render(request, 'geo_gas/edit_purchase.html', context)

edit_purchase.html

edit_purchase.html

    <form action="{% url 'geo_gas:edit_purchase' %}" method='post'>
    {% csrf_token %}
    {{ form.as_p}}
    <button name="Submit">save changes</button>
    </form>

已附加是返回的错误.

get() returned more than one items -- it returned 3!

在此处输入图片描述

推荐答案

只需阅读django文档 https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#get-object-or-404

just go through the django documentation https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#get-object-or-404

它内部调用get() https://docs.djangoproject.com/zh-CN/2.0/ref/models/querysets/#django.db.models.query.QuerySet.get

it internally calls get() https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.get

如果找到多个对象,它将引发MultipleObjectsReturned.

it will raise MultipleObjectsReturned if more than one object was found.

您需要将primary_key或任何其他fild传递给get_object_or_404,例如:get_object_or_404(MyModel,pk = 1)

you need to either pass primary_key or any other fild to get_object_or_404 like:get_object_or_404(MyModel, pk=1)

,或者如果您想要多个记录,则可以使用filter().

or you can use filter() if you want multiple records.

这篇关于Django:get()返回了多个项目-返回了3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆