简体   繁体   中英

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

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 . This means that self.patikent and self.doctor are not per se a patient and doctor object, but can be None as well.

You thus should cover the case where you these fields are 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 :
            names.append(f'{self.patient.firstname} {self.patient.lastname}')
        if :
            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. It means a consultation can have no patient and/or doctor?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM