I’m having this error in my code and I have used adjusted my code using NOT NULL constraint failed and other online resource but the problem persist.
Could you please tell me what I am doing wrong
MODELS.PY
class Auction(models.Model):
ABSTRACT = 'AB'
MODERN = 'MN'
ILLUSTRATION = 'IN'
select_category = [
('ABSTRACT', 'Abstract'),
('MODERN', 'Modern'),
('ILLUSTRATION', 'Illustration')
]
title = models.CharField(max_length=25)
description = models.TextField()
current_bid = models.IntegerField(null=False, blank=False)
image_url = models.URLField(verbose_name="URL", max_length=255, unique=True, null=True, blank=True)
category = models.CharField(
choices=select_category,
max_length=12,
default=MODERN,
null=True, blank=True
)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-created_at']
class Comment(models.Model):
auction = models.ForeignKey(Auction, related_name='comments', on_delete=models.CASCADE)
name = models.CharField(max_length=25)
description = models.TextField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return '%s, - %s' % {self.aunction.title, self.name}
class Meta:
ordering = ['-created_at']
FORMS.PY
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'description']
VIEWS.PY
def listing_detail(request, listing_id):
try:
detail = get_object_or_404(Auction, pk=listing_id)
except Auction.DoesNotExist:
messages.add_message(request, messages.ERROR, "This is not available")
return HttpResponseRedirect(reverse("index"))
bid_count = Bids.objects.filter(auction=listing_id).count()
bid_form= BidForm()
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.detail = detail
new_comment.save()
else:
comment_form = CommentForm()
else:
comment_form = CommentForm()
context = {'detail': detail,
'bid_count': bid_count,
'bid_form': bid_form,
'comment_form': comment_form}
return render(request, 'auctions/details.html', context)
TRACEBACK
IntegrityError at /detail/15/
NOT NULL constraint failed: auctions_comment.auction_id
|Request Method:|POST|
|Django Version:|4.0.2|
|Exception Type:|IntegrityError|
|Exception Value:|NOT NULL constraint failed: auctions_comment.auction_id|
|Exception Location:|C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py, line 416, in execute|
|Python Executable:|C:\Users\USER\AppData\Local\Programs\Python\Python310\python.exe|
|Python Version:|3.10.0|
|Python Path:|[‘C:\Users\USER\Downloads\commerce\commerce’, ‘C:\Users\USER\AppData\Local\Programs\Python\Python310\python310.zip’, ‘C:\Users\USER\AppData\Local\Programs\Python\Python310\DLLs’, ‘C:\Users\USER\AppData\Local\Programs\Python\Python310\lib’, ‘C:\Users\USER\AppData\Local\Programs\Python\Python310’, ‘C:\Users\USER\AppData\Roaming\Python\Python310\site-packages’, ‘C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages’]|
|Server time:|Mon, 21 Feb 2022 17:45:29 +0000|
-
C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py
, line 85, in _execute- return self.cursor.execute(sql, params)
…
- return self.cursor.execute(sql, params)
Local vars
-
C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py
, line 416, in execute- return Database.Cursor.execute(self, query, params)
…
Local vars
-
The above exception (NOT NULL constraint failed: auctions_comment.auction_id) was the direct cause of the following exception:
-
C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py
, line 47, in inner- response = get_response(request)
…
- response = get_response(request)
Local vars
-
C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py
, line 181, in _get_response- response = wrapped_callback(request, *callback_args, **callback_kwargs)
…
- response = wrapped_callback(request, *callback_args, **callback_kwargs)
Local vars
-
C:\Users\USER\Downloads\commerce\commerce\auctions\views.py
, line 139, in listing_detail- new_comment.save()