Home > Enterprise >  'modelBlog' object has no attribute 'CommentModel_set'
'modelBlog' object has no attribute 'CommentModel_set'

Time:03-04

Im try make blog detail and comment in one page but if im try posting comment i got error like this 'modelBlog' object has no attribute 'CommentModel_set' My views
def comment(request, id): blog = get_object_or_404(modelBlog, id=id) form = CommentForm(request.POST or None)

    heading = blog.title
    if request.method == 'POST':
        
       
        if form.is_valid():
            data={
                'name':request.POST['name'],
                'komentar':request.POST['komentar'],
            }
            blog.CommentModel_set.create(**data)
            return redirect('blog:detail', id)
            
    context = {
        'title':'Detail',
        'heading': heading,
        'blog':blog,
        'comment':comment,
        'form':form
    }
    return render(request, 'blog/detail.html', context)

And my model

class modelBlog(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    body = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True,)
    
    def __str__(self):
        return ('{}.{}').format(self.id, self.title)
class CommentModel(models.Model):
    user = models.ForeignKey('auth.User',default=1, on_delete=models.CASCADE)
    blog = models.ForeignKey(modelBlog, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    komentar = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True,)

My forms

class CommentForm(forms.ModelForm):
    class Meta:
        model = CommentModel
        fields = [
            'name',
            'komentar',
        ]
        widgets = {
            'name': forms.TextInput(attrs={'class':'form-control'}),
            'komentar': forms.Textarea(attrs={'class':'form-control'}),

        }
      
        }

My templates

<form action="{% url 'blog:comment' blog.id %}" method="post">
                 {% comment %}  {% endcomment %}
                {% csrf_token %}
                {% for forms in form %}
                <div >
                <label for="exampleFormControlInput1" >{{forms.label}}</label>
                <input type="{{forms.type}}" name="{{forms.name}}"  id="exampleFormControlInput1">
                </div>
                {% endfor %}
                <button type="submit" >Kirim</button>
           </form>

CodePudding user response:

Don't use uppercase! You must omit capital letters when use "_set"

blog.commentmodel_set.create(**data)

From the doc:

"If you don’t specify a related_name attribute for a field in an abstract base class, the default reverse name will be the name of the child class followed by '_set', just as it normally would be if you’d declared the field directly on the child class. For example, in the above code, if the related_name attribute was omitted, the reverse name for the m2m field would be childa_set in the ChildA case and childb_set for the ChildB field."

  • Related