0

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

Art
  • 2,836
  • 4
  • 17
  • 34
Prateek Gautam
  • 274
  • 2
  • 5
  • 23

3 Answers3

0

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

Fran Lendínez
  • 354
  • 2
  • 10
0

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)
lieahau
  • 498
  • 4
  • 9
  • It is still not working, the error is same -Instance of 'Post' has no 'comments' member and Class Post has no 'objects' member. – Prateek Gautam Aug 02 '19 at 06:23
  • is error exist in your browser or your IDE? maybe you can check this [link](https://stackoverflow.com/questions/51828667/class-has-no-objects-member-in-django) or this [link](https://stackoverflow.com/questions/45135263/class-has-no-objects-member) – lieahau Aug 02 '19 at 09:15
0
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.

Ankit Tiwari
  • 4,438
  • 4
  • 14
  • 41