I’m writing a forum using Django and I can’t figure out why after completing the form, the created Thread object doesn’t get an automatically assigned ID.
Error:
views.py:
from django.shortcuts import render, get_object_or_404
from .models import Board
from threads.models import Thread, Board, UserIP
from django.views import generic
from django.http import HttpResponse
from .forms import AddThreadForm
def empty(arg): #preventing boardThreadsView from rendering 2x
return HttpResponse("")
class BoardListView(generic.ListView): #homepage board listing
template_name = "index.html"
queryset = Board.objects.all()
def boardThreadsView(request,pk): #render thread on the board
arr = []
for thread in Thread.objects.all():
for b in thread.boards.all():
if pk == b.letter:
arr.append(thread)
return render(
request,
'board.html',
context={
'threads': arr,
'boards': Board.objects.all(),
'thisboard': Board.objects.filter(letter=pk)[0],
}
)
def addThread(request,pk): #add thread
ip = UserIP()
form = AddThreadForm()
thread = Thread()
print(thread.date, thread.id, thread.amountOfReplies)
if request.method == "POST":
form = AddThreadForm(request.POST)
if form.is_valid():
thread.boards.set(form.cleaned_data['boards'])
thread.content.set(form.cleaned_data['content'])
ip.set(request.META.get("REMOTE_ADDR"))
thread.userIP.set(ip)
# thread = Thread(content = form.cleaned_data['content'], boards=form.cleaned_data['boards'])
thread.save()
return render(
request,
'add_thread.html',
{
'form': form,
'thread': thread,
}
)
models.py:
import django.apps
from django.db import models
from django.utils import timezone
from boards.models import Board
from django.urls import reverse
# Create your models here.
class Thread(models.Model):
id = models.BigAutoField(primary_key=True, verbose_name='id')
content = models.CharField(help_text='Текст вашего сообщения', verbose_name='content', max_length=4096)
# image = models.ImageField(default=None, upload_to="post_picture")
date = models.DateTimeField(default=timezone.now, verbose_name='date')
amountOfReplies = models.IntegerField(default=0, verbose_name='amount-replies')
userIP = models.ForeignKey('UserIP', on_delete=models.SET_NULL, null=True, verbose_name='IP')
boards = models.ManyToManyField(Board)
class Meta:
ordering = ['-amountOfReplies']
def __str__(self):
return str(self.id)
def display_ip(self):
return self.userIP.ip
display_ip.short_description = 'IP'
def display_boards(self):
return ', '.join([b.letter for b in self.boards.all()])
display_boards.short_description = 'Boards'
class UserIP(models.Model):
ip = models.CharField(max_length=16)
def __str__(self):
return self.ip
# class Reply(models.Model):
# content = models.TextField(help_text='Текст вашего сообщения', verbose_name='content')
# thread = models.ForeignKey('Thread', on_delete=models.CASCADE, null=True, verbose_name='thread')
P.S. I have another app in my django project which has its own views and models, but they don’t really play a big role in what’s happening during the Thread creation, so I left them out.
I’m a newcomer, so bear with me