为什么我不能迭代这个查询集?

问题描述 投票:0回答:1

我试图使用一个基本的for循环迭代这个查询,它给了我一个“公司是不可迭代的”错误。有帮助吗?

def activate(request, uidb64, token, pk):
try:
    uid = force_text(urlsafe_base64_decode(uidb64))
    company = Company.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, Company.DoesNotExist):
    company = None
if company is not None and account_activation_token.check_token(company, token):
    pmm_date = Thursday.objects.get(pk=pk)
    company.is_registered = True
    company.save()
    pmm_date.assigned_company = company
    pmm_date.save()

    automatic_denial_list = Company.objects.exclude(company).filter(pmm_date=pmm_date)
    current_site = get_current_site(request)
    for company in automatic_denial_list:
        if company.email_one or company.email_two:
            mail_subject = 'Denied Pizza My Mind Visit'
            message = render_to_string('thursdays/denied_visit.html', {
                'user': company.name,
                'domain': current_site.domain,
            })
            to_email = company.email_one if company.email_one else company.email_two
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()
            # company.delete()
    return redirect('/')
else:
    return HttpResponse('Activation link is invalid!')

错误是在我定义automatic_denial_list时。谢谢!

python django django-queryset
1个回答
0
投票

因此,当您使用排除时,您必须进行字段查找。试试这个:

automatic_denial_list = Company.objects.exclude(id=company.id).filter(pmm_date=pmm_date)

这假设您的主键是id,这是django中的默认值

字段查找信息在这里:https://docs.djangoproject.com/en/2.0/ref/models/querysets/#field-lookups

© www.soinside.com 2019 - 2024. All rights reserved.