I have already tried multiple solutions find on other topics but nothing works.
I have a problem the foreign key in Django Admin. Impossible to see a records or add a new record.
I have a table convertion_factor
with no NULL values. see postgreSQL config
This is my model :
class convertion_factor(models.Model):
foodID = models.ForeignKey('food_name', on_delete=models.CASCADE)
measureID = models.ForeignKey('measure_name', on_delete=models.CASCADE)
conversionFactorValue = models.FloatField()
convFactorDateOfEntry = models.DateField(auto_now=False, auto_now_add=False)
In order to views the data in Django Admin, you have to add def __str__(self):
to the model. Ok let's do that :
class convertion_factor(models.Model):
foodID = models.ForeignKey('food_name', on_delete=models.CASCADE)
measureID = models.ForeignKey('measure_name', on_delete=models.CASCADE)
conversionFactorValue = models.FloatField()
convFactorDateOfEntry = models.DateField(auto_now=False, auto_now_add=False)
def __str__(self):
return self.conversionFactorValue
Now when I go to Django Admin and I click on the model convertion_factor
I can see the table just fine but when I try to click on a record to edit it, I have this error :
TypeError at /admin/kalo/convertion_factor/19505/change/
Exception Type: TypeError at /admin/kalo/convertion_factor/19505/change/
Exception Value: __str__ returned non-string (type float)
I have the impression this is an error due to the foreign key because with my others models without a foreign key, everythings work just fine.
In the function def __str__(self):
I have tried :
return self.foodID.__str__
but same error
return str(self.foodID)
but same error
Do you have any idea ?
Thanks for the help.
def __str__(self):
return str(self.conversionFactorValue)
This code works correctly with float value, Returns float value in the form of string value
I have added a screenshot below. django admin page view
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.