0
votes

I'm creating a basic blog webapp using django. The app starts without an error but when I click on drafts an error come up with AttributeError at /drafts 'Post' object has no attribute 'comments'

I've tried by putting comments = models.Manager() but then another error comes up saying Manager isn't accessible via post instances

my models.py

class Post(models.Model):
    
    author = models.ForeignKey('auth.User',on_delete=models.PROTECT)
    title = models.CharField(max_length=200)
    text = models.TextField()
    create_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True,null=True)
    # objects = models.Manager()
    # comments = models.Manager() 
    
    def publish(self):
        self.published_date = timezone.now
        self.save()
        
    def approve_comments(self):
        return self.comments.filter(approved_comment=True)

    def get_absolute_url(self):
        return reverse('blogapp:post_detail',kwargs={'pk':self.pk})

    def __str__(self): 
        return self.title

class Comment(models.Model):
    
    post = models.ForeignKey('blogapp.Post',on_delete=models.PROTECT)
    author = models.CharField(max_length=100)
    text = models.TextField(max_length=264)
    created_date = models.DateTimeField(default=timezone.now)
    approved_comment = models.BooleanField(default=False)

    def approve(self):
        self.approved_comment = True
        self.save()

    def get_absolute_url(self):
        return reverse('blogapp:post_list')

    def __str__(self):
        return self.text

my drafts view look something like

class DraftListView(LoginRequiredMixin,ListView):
    login_url = '/login/'
    redirect_field_name = 'blogapp/post_list.html'
    model = Post

    def get_queryset(self):
        return Post.objects.filter(published_date__isnull=True).order_by('create_date')

I'm using 'comments' variable in another html and views file. And the same error arise with 'objects' while executing the line

Post.objects.filter(published_date__isnull=True).order_by('create_date')

in my views.py file

3
The error indicates that you likely created a Post object with the name Post. - Willem Van Onsem

3 Answers

0
votes

Follow this steps:

1 .- You must define the model Comment

2 .- The Comment model must define a foreign key to Post

3 .- This foreign key needs the attribute related_name in order to be accessible via Post model usign the word comments

+Info: https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.ForeignKey.related_name

0
votes

use related_name attribute
and you don't need to use 'blogapp.Post' if it is in same file, but just Post instead

class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.PROTECT)


in case if it is on different app/models file, use blogapp.models.Post instead

class Comment(models.Model):
    post = models.ForeignKey(blogapp.models.Post, related_name='comments', on_delete=models.PROTECT)
0
votes
def approve_comments(self):
    return self.**comments**.filter(approved_comment=True)

Hello, hackerWorld I think error is here in approve_comments you return the comments which is not attribute of your Post class.