[英]why am i getting error of AttributeError at /admin/main/consultation/ 'NoneType' object has no attribute 'lastname'
in my admin page when i click on consultation i am getting this error在我的管理页面中,当我单击咨询时出现此错误
#model.py #model.py
class doctor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
is_patient = models.BooleanField(default=False)
is_doctor = models.BooleanField(default=True)
firstname = models.CharField(max_length = 50)
lastname = models.CharField(max_length = 50,default='')
dob = models.DateField()
address = models.CharField(max_length = 100)
countrycode=models.CharField(max_length=5)
mobile_no = models.CharField(max_length = 15)
gender = models.CharField(max_length = 10)
registration_no = models.CharField(max_length = 20)
specialization = models.CharField(max_length = 30)
rating = models.IntegerField(default=0)
qualification = models.CharField(max_length=100,default='')
year_of_registration = models.CharField(max_length=50,default='')
medical_experience = models.CharField(max_length=50,default='')
medical_council=models.CharField(max_length=80,default='')
charges = models.IntegerField(default=0)
profilephoto = models.ImageField(null=True,blank=True,upload_to="dprofile/")
def __str__(self):
return "Dr. " + self.firstname +" "+ self.lastname
class consultation(models.Model):
patient = models.ForeignKey(patient ,null=True, on_delete=models.SET_NULL)
doctor = models.ForeignKey(doctor ,null=True, on_delete=models.SET_NULL)
diseaseinfo = models.OneToOneField(diseaseinfo, null=True, on_delete=models.SET_NULL)
consultation_date = models.DateField()
status = models.CharField(max_length = 20)
def __str__(self):
return self.patient.firstname + " "+self.doctor.lastname + '- Dr.' +self.doctor.firstname + ' ' +self.doctor.lastname
You consultation
has two nullable fields patient
and doctor
.您的
consultation
有两个可为空的字段patient
和doctor
。 This means that self.patikent
and self.doctor
are not per se a patient
and doctor
object, but can be None
as well.这意味着
self.patikent
和self.doctor
本身不是patient
和doctor
object,但也可以是None
。
You thus should cover the case where you these fields are None
:因此,您应该涵盖这些字段为
None
的情况:
class consultation(models.Model):
patient = models.ForeignKey(patient ,null=True, on_delete=models.SET_NULL)
doctor = models.ForeignKey(doctor ,null=True, on_delete=models.SET_NULL)
diseaseinfo = models.OneToOneField(diseaseinfo, null=True, on_delete=models.SET_NULL)
consultation_date = models.DateField()
status = models.CharField(max_length = 20)
def __str__(self):
names = []
if
self.patient is not None:
names.append(f'{self.patient.firstname} {self.patient.lastname}')
if
self.doctor is not None:
names.append(f'Dr. {self.doctor.firstname} {self.doctor.lastname}')
return ' - '.join(names)
It however looks odd that patient
and doctor
can be NULL
in the first place.然而,看起来很奇怪,
patient
和doctor
首先可以是NULL
。 It means a consultation can have no patient and/or doctor?这意味着咨询可以没有病人和/或医生?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.