【问题标题】:How to call an object using post data from a list of objects in Django?如何使用 Django 中对象列表中的发布数据调用对象?
【发布时间】:2014-01-28 15:52:44
【问题描述】:

Django 1.6/Python 2.7。

我正在制作一个应用程序,用户在该应用程序中从一组公民对象中对“公民”进行投票,该公民成为最佳公民。只有拥有 3 个或更多“投票”的公民才有资格成为“最佳公民”。我在管理员中添加公民和投票,所以我们不会在这里处理。这些是我的模型:

best_app/models.py

from django.db import models

class Citizen(models.Model):
    name = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    can_best = models.BooleanField(False)

    def __unicode__(self):
        return self.name

class Best_Citizen(models.Model):
    name = models.CharField(max_length=200)
    citizen = models.OneToOneField(Citizen)

    def __unicode__(self):
        return self.name

我在 choose_best 视图中遇到了问题,在该视图中,应通过主键缩小所选公民的范围。我不能在对象列表上调用 get() ,但这似乎是我应该做的。我也试过过滤器来缩小视图,语法不正确。

from best_app.models import Citizen, Best_Citizen
from django.shortcuts import render, get_object_or_404, get_list_or_404

def index(request):

    citizens = Citizen.objects.all()
 #   citizens = get_list_or_404(Citizen)
    for citizen in citizens: 
        if citizen.votes >= 3:
            citizen.can_best = True
            citizen.save()

    return render(request, 'best_app/index.html', {'citizens':citizens})

def detail(request, citizen_id):

    try:
        citizen = Citizen.objects.get(pk=citizen_id)
    except Citizen.DoesNotExist:
        print "raise Http404"
    return render(request, 'best_app/detail.html', {'citizen':citizen})
 #   return HttpResponse("You're looking at poll %s." % citizen.name)

def choose_best(request):

    best_candidates = get_list_or_404(Citizen, can_best=True)       # narrow down the candidates for best citizen to those with >= 3 votes

    if request.method == 'POST':

        try:
            selected_choice = best_candidates.get(pk=request.POST['choice'])
        except (KeyError, Citizen.DoesNotExist):

            return render(request, 'index.html')
        else:   
            Best_Citizen.objects.all().delete()                 # Current best citizen is deleted to make way for new best citizen
            new_best = Best_Citizen(citizen=selected_choice)    
            new_best.save()
            return render(request, 'best_app/index.html', {'new_best':new_best})

    else:
        return render(request, 'best_app/choose_best.html', {'best_candidates':best_candidates})        

我收到错误消息“列表属性没有属性获取”。如果过滤和获取不起作用,我如何通过主键选择所选的“最佳候选人”?
谢谢!

【问题讨论】:

    标签: django list post python-2.7 filter


    【解决方案1】:

    正如documentation for get_list_or_404 所述,它将结果转换为列表。如果您想进一步过滤它,那就不好了:所以不要使用那个快捷方式。

     best_candidates = Citizen.objects.filter(can_best=True)
    

    【讨论】:

    • 他也可以这样做 selected_choice = get_object_or_404(Citizen, pk=request.POST['choice']) 而不是 selected_choice = best_candidates.get(pk=request.POST['choice']) (并且不理会 get_list_or_404 部分)
    • @Daniel Roseman Filter 是我尝试的第一件事,但我不断收到错误消息'invalid literal for int() with base 10:''。经过进一步检查,这与 selected_choice = best_candidates.get(pk=request.POST['choice']) 相关联
    猜你喜欢
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    相关资源
    最近更新 更多