'Error during template rendering / __str__ returned non-string (type NoneType)
I was trying to edit from Django admin panel then this error occure Error: Error during template rendering In template D:\Django\new\venv\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19 str returned non-string (type NoneType) enter image description here
models.py looks like:
class hotel(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
hotel_name = models.CharField(max_length=500, null=True)
registration_no = models.CharField(max_length=500, null=True)
company_PAN_number = models.CharField(max_length=500, null=True)
registered_owner_name = models.CharField(max_length=500, null=True)
gender = models.CharField(max_length=100, blank=True, null=True, choices=gender_choice)
email = models.EmailField(max_length=500, null=True)
phone_no = models.CharField(max_length=500, null=True)
website = models.URLField(max_length=500, null=True)
registration_certificate = models.ImageField(null=True, upload_to='documents/%y/%m/%d/')
PAN_certificate = models.ImageField(null=True, upload_to='documents/%y/%m/%d/')
citizen_id_front = models.ImageField(null=True, upload_to='citizen_id/%y/%m/%d/')
citizen_id_back = models.ImageField(null=True, upload_to='citizen_id/%y/%m/%d/')
verified_hotel = models.BooleanField(default=False, null=True)
def __str__(self):
return self.hotel_name
class hotel_application_status(models.Model):
hotel = models.ForeignKey(hotel, null=True, on_delete=models.CASCADE)
resubmitted = models.BooleanField(default=False, null=True)
comment = models.CharField(max_length=500, null=True)
def __str__(self):
return self.hotel.hotel_name
Solution 1:[1]
I think this is probably what you want
def __str__(self):
return self.hotel.hotel_name if self.hotel and self.hotel.hotel_name else ''
Solution 2:[2]
Thank you all for your amazing support!!
I solve the above error by deleting the null row from the hotel database. The above error was occurring due, to Null Field. I was using Django signals in this project and when the user is created the hotel is also created with Null fields.
It works fine for Django forms but when you use the Django admin panel to add new data the above errors occur.
Error Details
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Lalit mahato |