Skip to content
Advertisement

Django Form does not save properly

I want to create a comment system. I can take username, doc_id, and comp_name, but I cannot get the comment_others. I think there is a problem saving the form. How can I fix it?

views.py

def ocr(request, id):
    pdf = get_object_or_404(Pdf, id=id)
    approval = ApprovalProcess(user_id=request.user, highest_rank=1)
    current_user = request.user
    userP = UserProfile.objects.get_or_create(username=current_user)

    if request.method == 'POST':
        form_2 = CommentForm(request.POST or None, instance=pdf)

        if form_2.is_valid():
            form_2.instance.username=request.user
            form_2.instance.comp_name = userP[0].company
            form_2.instance.doc_id = pdf
            form_2.save()

    else:
        form_2 = CommentForm()

    comment_obj = CommentFromOthers.objects.filter(doc_id=pdf).order_by("-created_date")

.....

models.py

class CommentFromOthers(models.Model):
    comp_name = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True)
    doc_id = models.ForeignKey(Pdf, on_delete=models.DO_NOTHING, null=True)
    comment_others = RichTextField(blank=True)
    created_date = models.DateTimeField(default=datetime.now())
    username = models.ForeignKey(UserProfile, on_delete=models.CASCADE)

forms.py

class CommentForm(forms.ModelForm):
    comments = RichTextField

    class Meta:
        model = CommentFromOthers
        fields = ('comment_others',)

template.html

<div class="card">
                                        <div class="card-header">
                                            <div class="card-title">Risk Rating & Credit Limit</div>
                                        </div>
                                        <div class="card-body">
                                            {% for comment in comment_obj %}
                                        <li>
                                            <h5>{{ comment.username }} - {{ comment.created_date }}</h5>
                                            <h4>{{ comment.comment_others|safe}}</h4>
                                        </li>
                                        {% endfor %}
                                        </div>
                            </div>

And there are two forms on the same page. When I save this form, the other form disappears. Why it could be happening?

if request.method == 'POST':
    form = PdfRiskForm(request.POST, request.FILES, instance=pdf)

    if form.is_valid():
        form.save()
        ...
        approval.save()

else:
    form = PdfRiskForm()

Note: RichTextField is the text field of django-ckeditor. But the problem is not relevant to that because when I change it as charfield it still doesn’t save the comment_others field.

Advertisement

Answer

forms.py

comments = forms.CharField(widget=forms.Textarea)

views.py

if request.method == 'POST':
    form_2 = CommentForm(request.POST or None, instance=id) #or user.id

    if form_2.is_valid():
        
        form_2.save()

by eliminating those lines in views.py will automatically save the fields in your database.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement