# Integrity Error Not NULL Constraint failed

**URL:** <https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096>\
**Category:** Using Django\
**Created:** [June 23, 2020, 6:23pm UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096 "2020-06-23T18:23:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![varinder-pal](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@varinder-pal](https://forum.djangoproject.com/u/varinder-pal)\
**Post date:** [June 23, 2020, 6:23pm UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096/1 "2020-06-23T18:23:10Z")

</div>

# \*\*My models\*\*

from django.db import models

```
class Product(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(default='write description')
    price = models.DecimalField(decimal_places=2, max_digits=10000)

    def __str__ (self):
        return self.title

class Comment(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    comment = models.TextField(default='Write ur comments')

    def __str__ (self):
    return self.comment

```

## \*\*My views code\*\*

```
def home_view(request):
    product_list = Product.objects.order_by('title')
    context = {'product_list': product_list}
    return render(request, 'my_rev/home_view.html', context)

def detail_view(request, product_id):
    product = Product.objects.get(pk=product_id)
    comment_form = CommentForm(request.POST, instance=product)
    if comment_form.is_valid():
        comment_form.save()
        comment_form = CommentForm()
    context = {'product': product, 'comment_form': comment_form}
    return render(request, 'my_rev/detail_view.html', context)

def create_product_view(request):
   form = ProductForm(request.POST or None)
   if form.is_valid():
    form.save()
    form = ProductForm()
   context = {
    'form': form
    }
   return render(request, 'my_rev/create_view.html', context)

 def user_comment_view(request):
    comment_form = CommentForm(request.POST or None)
 if comment_form.is_valid():
    comment_form.save()
    comment_form = CommentForm()
 context2 = {
    'comment_form': comment_form
    }
 return render(request, 'my_rev/comment_view.html', context2)

```

### URL patterns ``` urlpatterns = [ path('', home_view, name='Home'), path('<int:product_id>/', detail_view, name='detail'), path('create/', create_product_view, name='create'), path('comment/', user_comment_view, name='add comment'), path('admin/', admin.site.urls), ```

]

#### detail\_view.html

```
<body>
<h1>{{ product.title }}</h1>
<p>
<p><strong>PRODUCT DESCRIPTION</strong></p>
    {{ product.description }}
<p><strong>Price: </strong>Rs.{{ product.price }}</p>
<ul>
{% for comment in product.comment_set.all %}
    <li>{{ comment.comment|capfirst }}</li>

{% endfor %}
        </ul>
<p><a href="/comment/"><strong>ADD COMMENT</strong></a></p>

```

##### comment\_view.html

```
<body>
<h1>{{product.title}}</h1>
<form method="POST"> {% csrf_token %}
{{ comment_form.as_p }}
<input type="submit" value="Save"/>

```

I am getting the Integrity error NOT NULL Constrained failed after the comment is submitted, it is not saved as well as i get the error

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [June 23, 2020, 8:34pm UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096/2 "2020-06-23T20:34:30Z")

</div>

Please take time to format your code nicely on the forum to help people answer your questions. Use the “HTML tag” button in the editor.

---

<div class="post-metadata">

**Author:** ![varinder-pal](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@varinder-pal](https://forum.djangoproject.com/u/varinder-pal)\
**Post date:** [June 24, 2020, 11:06am UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096/3 "2020-06-24T11:06:45Z")

</div>

Hi, I have edited my post can you sort out the problem

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 24, 2020, 12:06pm UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096/4 "2020-06-24T12:06:47Z")

</div>

First thing that jumps out at me is that your link in detail\_view.html for ADD COMMENT doesn’t include the product id field in the URL. That means your user\_comment\_view doesn’t have the information it needs to assign to the product variable in the Comment model.

(I’ll also mention at this point that I don’t see where in any of your views you’re checking to see if the request is a GET, looking to get the blank form, or a POST, having data being submitted in the form. I suggest you review the documentation on [how to handle forms](https://docs.djangoproject.com/en/3.0/topics/forms/#working-with-forms) in Django, especially the [view docs](https://docs.djangoproject.com/en/3.0/topics/forms/#the-view).)

Ken

---

<div class="post-metadata">

**Author:** ![varinder-pal](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@varinder-pal](https://forum.djangoproject.com/u/varinder-pal)\
**Post date:** [June 24, 2020, 5:30pm UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096/5 "2020-06-24T17:30:21Z")

</div>

Hi Ken thanks for your reply I have changed views function as follows:

```auto
def detail_view(request, product_id):
    product = Product.objects.get(pk=product_id)
    if product:
        comment_form = CommentForm(request.POST or None, instance=product)
        if comment_form.is_valid():
            comment_form.save()
            comment_form = CommentForm()
context = {'product': product, 'comment_form': comment_form}
return render(request, 'my_rev/detail_view.html', context)

```

Further I have edited my details\_view. html as follows:

```auto
<body>
<h1>{{ product.title }}</h1>
<p>
<p><strong>PRODUCT DESCRIPTION</strong></p>
    {{ product.description }}
<p><strong>Price: </strong>Rs.{{ product.price }}</p>
<ul>
   {% for comment in product.comment_set.all %}
    <li>{{ comment.comment|capfirst }}</li>

{% endfor %}
        </ul>

<form method="POST"> {% csrf_token %}
    {{ comment_form.as_p }}
    <input type="submit" value="Save"/>
</form>

```

Now I am not getting error but my input in the form is not being saved, can you please guide me on this.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 24, 2020, 5:46pm UTC](https://forum.djangoproject.com/t/integrity-error-not-null-constraint-failed/3096/6 "2020-06-24T17:46:53Z")

</div>

From above:

> [@KenWhitesell](#):
>
> (I’ll also mention at this point that I don’t see where in any of your views you’re checking to see if the request is a GET, looking to get the blank form, or a POST, having data being submitted in the form. I suggest you review the documentation on [how to handle forms](https://docs.djangoproject.com/en/3.0/topics/forms/#working-with-forms) in Django, especially the [view docs](https://docs.djangoproject.com/en/3.0/topics/forms/#the-view).)

Also, if you haven’t already done so, I would suggest you work your way through the official [Django tutorial](https://docs.djangoproject.com/en/3.0/intro/tutorial01/#writing-your-first-django-app-part-1). That will provide you with some specific examples of working with forms in views.
