简体   繁体   English

为什么我在 /admin/main/consultation/'NoneType' object 没有属性 'lastname' 处出现 AttributeError 错误

[英]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有两个可为的字段patientdoctor This means that self.patikent and self.doctor are not per se a patient and doctor object, but can be None as well.这意味着self.patikentself.doctor本身不是patientdoctor 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.然而,看起来很奇怪, patientdoctor首先可以是NULL It means a consultation can have no patient and/or doctor?这意味着咨询可以没有病人和/或医生?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到“AttributeError: 'NoneType' object has no attribute 'get'” - why am i getting "AttributeError: 'NoneType' object has no attribute 'get' " 为什么我收到“AttributeError: 'NoneType' object has no attribute 'send'”错误 - Why am I getting a `AttributeError: 'NoneType' object has no attribute 'send'` Error 为什么我在执行 PCA 时遇到此错误:- AttributeError: 'NoneType' object has no attribute 'split' - Why i am getting this error while doing PCA :- AttributeError: 'NoneType' object has no attribute 'split' 为什么我会收到 AttributeError: 'NoneType' object has no attribute 'send' 以及如何避免它? - Why am I getting AttributeError: 'NoneType' object has no attribute 'send' and how to avoid it? Web Scraper:为什么会出现AttributeError:'NoneType'对象没有属性'text'? - Web Scraper: Why am I getting AttributeError: 'NoneType' object has no attribute 'text'? 为什么我收到 AttributeError: “'NoneType' object has no attribute 'get'” 与 Python 和 Tkinkter? - Why I am getting AttributeError: “'NoneType' object has no attribute 'get'” with Python and Tkinkter? 我收到类似 AttributeError: 'NoneType' object has no attribute 'text' 的错误 - I am getting error like AttributeError: 'NoneType' object has no attribute 'text' 我在“AttributeError:'NoneType'对象中没有属性'get_all_permissions'中收到这些错误 - I am getting these error in " AttributeError: 'NoneType' object has no attribute 'get_all_permissions' 我不断收到错误:AttributeError: 'NoneType' object has no attribute 'strip' - I keep getting the error: AttributeError: 'NoneType' object has no attribute 'strip' 为什么错误 - AttributeError: 'NoneType' object 没有属性 - Why error - AttributeError: 'NoneType' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM