1

I need some help. I'm writing an app with Django 2.1 and python 3.7

I am trying to add a new Comment that it is associated to an Article through foreign key.

The url of the screen to add a new comment is http://127.0.0.1:8000/articles/1/comment/new/ so the url has the foreign key.

When I try a save the comment it returns the error below:

IntegrityError at /articles/1/comment/new/
NOT NULL constraint failed: articles_comment.article_id
Request Method: POST
Request URL:    http://127.0.0.1:8000/articles/1/comment/new/
Django Version: 2.0.6
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: articles_comment.article_id
Exception Location: /Users/fabiodesimoni/.local/share/virtualenvs/news-s2R6SLTq/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 303
Python Executable:  /Users/fabiodesimoni/.local/share/virtualenvs/news-s2R6SLTq/bin/python
Python Version: 3.7.1
Python Path:    
['/Users/fabiodesimoni/Development/Python/Django/news',
 '/Users/fabiodesimoni/.local/share/virtualenvs/news-s2R6SLTq/lib/python37.zip',
 '/Users/fabiodesimoni/.local/share/virtualenvs/news-s2R6SLTq/lib/python3.7',
 '/Users/fabiodesimoni/.local/share/virtualenvs/news-s2R6SLTq/lib/python3.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/Users/fabiodesimoni/.local/share/virtualenvs/news-s2R6SLTq/lib/python3.7/site-packages']
Server time:    Tue, 18 Dec 2018 10:05:07 +0000

If I add the field Article into the Comment screen, the screen is loaded with the field Article blank and I am have to select the Article in order to save it with success. I want to do it automatically because the article id is in the url.

I hope all information are here and in case it needed the code the source code is https://github.com/fabiofilz/newspaper_app

Model.py:

    from django.contrib.auth.models import User
    from django.conf import settings
    from django.contrib.auth import get_user_model
    from django.db import models
    from django.urls import reverse

    class Article(models.Model):
        title = models.CharField(max_length=255)
        body = models.TextField()
        date = models.DateTimeField(auto_now_add=True)
        author = models.ForeignKey(get_user_model(),on_delete=models.CASCADE,)

        REQUIRED_FIELDS = ['author', 'title', 'body']

        def __str__(self):
            return '(id: ' + str(self.id) + ') ' + self.title

        def get_absolute_url(self):
            return reverse('article_detail', args=[str(self.id)])

    class Comment(models.Model):
        article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='comments')
        comment = models.CharField(max_length=140)
        author = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, )

        def __str__(self):
            return '(id: ' + str(self.id) + ') ' + self.comment

        def get_absolute_url(self):
            reverse('comment_detail', args=[str(self.id)])

url.py

urlpatterns = [
    path('', views.ArticleListView.as_view(), name='article_list'),
    path('<int:pk>/edit/', views.ArticleUpdateView.as_view(), name='article_edit'),
    path('<int:pk>/', views.ArticleDetailView.as_view(), name='article_detail'),
    path('<int:pk>/delete/', views.ArticleDeleteView.as_view(), name='article_delete'),
    path('new/', views.ArticleCreateView.as_view(), name='article_new'),

    path('<article_pk>/comment/<comment_pk>', views.CommentDetailView.as_view(), name='comment_detail'),
    url(r'^(?P<pk>\d+)/comment/(?P<comment_pk>\d+)/delete/$', views.CommentDeleteView.as_view(), name='comment_delete'),
    url(r'^(?P<pk>\d+)/comment/(?P<comment_pk>\d+)/edit/$', views.CommentUpdateView.as_view(), name='comment_edit'),
    path('<article_pk>/comment/new/', views.CommentCreateView.as_view(), name='comment_new'),
]

view.py

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from . import models
from django.utils import timezone
from django.shortcuts import redirect

# COMMENT VIEWS

class CommentCreateView(LoginRequiredMixin, CreateView):
    model = models.Comment
    template_name = 'comment_new.html'
    fields = ['comment'] #, 'article']
    success_url = reverse_lazy('article_list')
    login_url = 'login'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

Thank you

Fabio de Simoni
  • 81
  • 1
  • 1
  • 8
  • So where are you having problems doing this? You already know how to assign the author automatically, what's stopping you doing the same thing with the article? – Daniel Roseman Dec 18 '18 at 10:27
  • Please check of this work for you. https://stackoverflow.com/questions/50580882/django-create-and-update-views-foreign-key-field – gledsoul Dec 18 '18 at 10:47
  • Thank you for you reply. I have a previous screen that has all Articles and next to each article there is a link "add a comment" about the article. When the comment page is loaded using the http://127.0.0.1:8000/articles/1/comment/new/, the field Article is not populated. I think it is the issue, but I don't know how to do it (I am guessing this is the issue, because when I add the field in the screen the field Article is blank). – Fabio de Simoni Dec 18 '18 at 11:36

1 Answers1

2

I found a similar issue:

NOT NULL constraint failed Django CreateView

class CommentCreateView(LoginRequiredMixin, CreateView):
    model = models.Comment
    template_name = 'comment_new.html'
    fields = ['comment'] #, 'article']
    success_url = reverse_lazy('article_list')
    login_url = 'login'

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.article = get_object_or_404(models.Article, 
                                                  id=self.kwargs.get('article_pk')) # new line
        return super().form_valid(form)
Fabio de Simoni
  • 81
  • 1
  • 1
  • 8