Django Topic dropdown displays Topic object(1) but not the name of the object
class Topic(models.Model):
"""A topic the user is learning about."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
class Entry(models.Model):
"""Something specific learned about a topic"""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""Return a simple string representation of the entry."""
return f"{self.text[:50]}..."
Above is the code in the models.py, I’m very knew to django but I an alright understanding of python. Cant really seem to figure out why when I navigate to the website the topics name doesnt appear but instead it says topic object(1) –> topic object(2)
Dokkaebi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You need to return a string representation of your Topic model. You should always return a nice, human-readable representation of the model from the __str__()
method.
Add this to your Topic model just as you did to Entry:
def __str__(self):
"""Return a string representation of the topic."""
return self.text
See https://docs.djangoproject.com/en/5.0/ref/models/instances/#str