1

I'm working on this project but I got the following error comm = doctor_detail.comments.filter(active=True) AttributeError: 'Doctor' object has no attribute 'comments' however I think that everything is alright

this is my models.py

class Comments(models.Model):
    co_name = models.CharField(max_length=50, verbose_name="الاسم ")
    co_email = models.EmailField(
        max_length=50, verbose_name="البريد الالكتروني")
    co_body = models.TextField(max_length=400, verbose_name='التعليق')
    created_dt = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)
    post = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='comments')

    def __str__(self):
        return 'علق {} على {}'.format(self.co_name, self.post)

this is my views.py

def doctor_detail(request, slug):
    # استدعاء جميع البينات اللي في البروفايل
    doctor_detail = get_object_or_404(Doctor, slug=slug)
    comm = doctor_detail.comments.filter(active=True)
    form = Commentair()
    if request.method == 'POST':
        form = Commentair(data=request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.post = doctor_detail
            new_comment.save()
            form = Commentair()
    else:
        form = Commentair()
    return render(request, 'user/doctor_detail.html', {'doctor_detail': doctor_detail,
                                                       'comm': comm,'form': form
                                                       })

I really don't know why I'm facing this error because the related name is available and I think everything is okay .please if there is an answer to this write it and explain it to me. thank you

1 Answers1

0
### You done mistake in here comments -> Comments c will be capital in filter

def doctor_detail(request, slug):
    # استدعاء جميع البينات اللي في البروفايل
    doctor_detail = get_object_or_404(Doctor, slug=slug)
    comm = doctor_detail.Comments.filter(active=True)
    form = Commentair()
    if request.method == 'POST':
        form = Commentair(data=request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.post = doctor_detail
            new_comment.save()
            form = Commentair()
    else:
        form = Commentair()
    return render(request, 'user/doctor_detail.html', {'doctor_detail': doctor_detail,
                                                       'comm': comm,'form': form
                                                       })
Parth Mehta
  • 191
  • 6