[Fixed]-Django: 'Thoughts' object has no attribute 'choice_set' – Trouble accessing a choice set

1👍

To retrieve all the Comments related to a Thought. You can do the following:

Thoughts.objects.get(pk=thought_num).comments_set.all()

If you would like to override the default related_name (“comments_set”). You can do the following:
original_post = models.ForeignKey(Thoughts, default=0, related_name='choice_set')

0👍

When you make a ForeignKey the default related name becomes the lower case name of the current class + “_set” so for your project should be:

get_post = Thoughts.objects.get(pk=thought_num)
comments = get_post.comments_set.all()

Or you could even create a custom related name instead of the default:

class Thoughts(models.Model):
    name = models.CharField(max_length=30)
    thought = models.CharField(max_length=500)
    class Thoughts(models.Model):
    name = models.CharField(max_length=30)
    thought = models.CharField(max_length=500)

class Comments(models.Model):
    name = models.CharField(max_length=30)
    comment = models.CharField(max_length=200)
    original_post = models.ForeignKey(Thoughts, default=0, related_name='comments')

so you can get your comments like this:

get_post = Thoughts.objects.get(pk=thought_num)
comments = get_post.comments.all()

Leave a comment