gpt4 book ai didi

python - 我遇到了有关 AttributeError 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:44 27 4
gpt4 key购买 nike

AttributeError at /addpatient_to_db
'QuerySet' object has no attribute 'wardno'

请求方式:POST

请求网址:http://127.0.0.1:8000/addpatient_to_db

Django 版本:2.2.5

异常类型:AttributeError

异常值:“QuerySet”对象没有属性“wardno”

异常位置:C:\Users\Saurabh Patil\Desktop\SanjeevniHospital\admininterface\views.py in addpatent_to_db, line 114

Python 可执行文件:C:\Users\Saurabh Patil\AppData\Local\Programs\Python\Python37\python.exe

Python版本:3.7.4

Python 路径:

['C:\\Users\\Saurabh Patil\\Desktop\\SanjeevniHospital',
'C:\\Users\\Saurabh '
'Patil\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\Saurabh Patil\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\Saurabh Patil\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\Saurabh Patil\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\Saurabh Patil\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\Saurabh '
'Patil\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

服务器时间:2020年1月26日星期日12:07:40 +0000

这是我的代码

views.py

def addpatient_to_db(request):
if request.method == 'POST':
name = request.POST['name']
age = request.POST['age']
sex = request.POST['sex']
address = request.POST['address']
contno = request.POST['contno']
wardno = request.POST['wardno']
bedno = request.POST['bedno']
doa = request.POST['doa']
docass = request.POST['docass']
pii = request.POST['pii']

alldata = patientdetails.objects.all()

if alldata.wardno == wardno and alldata.bedno == bedno: # <---- This is the issuing line
return render(request, "addpatient.html")
else:
addp = patientdetails(name=name, age=age, sex=sex, address=address, mobno=contno,
wardno=wardno, bedno=bedno, dateofallot=doa, docass=docass, illness_issue=pii)
addp.save()
return redirect('addpatient')
else:
return render(request, 'addpatient.html')

models.py

from django.db import models

# Create your models here.

class patientdetails(models.Model):
name = models.CharField(max_length=50)
age = models.CharField(max_length=3)
sex = models.CharField(max_length=10)
address = models.CharField(max_length=100)
mobno = models.CharField(max_length=10)

wardno = models.CharField(max_length=3)
bedno = models.CharField(max_length=3)
dateofallot = models.CharField(max_length=10)

docass = models.CharField(max_length=50)
illness_issue = models.CharField(max_length=50)

最佳答案

alldata.wardn 不起作用,因为它是一个包含对象列表的 QuerySet 。如果您已经知道 alldata 仅包含一个对象,那么您可以使用 first() 来检索该对象:

alldata = patientdetails.objects.first()

否则,如果 QuerySet 中有多个对象,您可以简单地迭代 alldata:

alldata = patientdetails.objects.all()

for patient in alldata:
if patient.wardno == wardno and patient.bedno == bedno:
return render(request, "addpatient.html")
else:
addp = patientdetails(name=name, age=age, sex=sex, address=address, mobno=contno,
wardno=wardno, bedno=bedno, dateofallot=doa, docass=docass, illness_issue=pii)
addp.save()
return redirect('addpatient')

关于python - 我遇到了有关 AttributeError 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59918219/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com