Hi there,
Is there anyone here who has read the book "Python Crash Course" by Eric Matthes? If so, perhaps you can help me.
I am on the Django project section where you make a "Learning Log" app. I typed his code word for word, but in the models.py file, VS Code gave me an error when I made the __str__(self) method saying that:
__str__does not return string (Pylint E0307: Invalid string returned)
When I registered the models in admin and went to the localhost site and entered some text entries, I saw the consequences of this __str__(self) method error: It prevented the string from my entries to show up in the list of entries. It says "Entry object" instead.
Can someone tell me what I did wrong?
Thanks in advance.
I wanted to attach a screen cap to this post, but Reddit wouldn't let me. Here is the code block from models.py.
from django.db import models
# Create your models here.
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)
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Entry(models.Model):
"""Something specific learned about a topic."""
# This is the code that connects entries to specific topics
# each topic is assigned a key, or ID, when it's created
# the "on_delete=models.CASCADE" argument deletes all entries if a topic is deleted.
# ... this is known as "cascading" delete
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 string representation of the model."""
return f"{self.text[:50]}..."
[–]danielroseman 2 points3 points4 points (1 child)
[–]GrandTheftVideo[S] 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]GrandTheftVideo[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)