I am creating a website where people under the topics set by the administrator will be able to make their entries. Each record is linked to a user account, but when creating a new record, an error appears - NOT NULL constraint failed: menu_description.topic_id
menu/views.py
from django.shortcuts import render, redirect
from .models import Topic, Description
from .forms import DescriptionForm
from django.contrib.auth.decorators import login_required
def index(request):
topic = Topic.objects.all
context = {'topic': topic}
return render(request, 'Menu/Menu.html', context)
@login_required
def description(request, topic_id):
topic = Topic.objects.get(id=topic_id)
description = topic.description_set.filter(owner=request.user).order_by('date_added')
context = {'description': description, 'topic': topic}
return render(request, 'Menu/Description.html', context)
@login_required
def new_description(request, topic_id):
topic = Topic.objects.get(id=topic_id)
if request.method != 'POST':
form = DescriptionForm()
else:
form = DescriptionForm(data=request.POST)
if form.is_valid():
new_description = form.save(commit=False)
new_description.owner = request.user
new_description.save()
return redirect('description', topic_id=topic_id)
context = {'topic': topic, 'form': form}
return render(request, 'Menu/new_description.html', context)
@login_required
def edit_description(request, entry_id):
description = Description.objects.get(id=entry_id)
topic = description.topic
if request.method != 'POST':
form = DescriptionForm(instance=description)
else:
form = DescriptionForm(instance=description, data=request.POST)
if form.is_valid():
form.save()
return redirect('description', topic_id=topic.id)
context = {'description': description, 'topic': topic, 'form': form}
return render(request, 'Menu/Edit_description.html', context)
@login_required
def delete_description(request, description_id):
description = Description.objects.get(id=description_id)
topic = description.topic
description.delete()
return redirect('description', topic_id=topic.id)
my code fsdfsfdsfsfas sdfsadklnfajsnflksanfgiljsglidsehseoigusdhoigusdfhoigsdhoigudsfhgoidsfhgokjfdshdsoiughsodfighosidhgosidfghsdoiughdfoigushdfiogsfdhikg
menu/models.py
from django.db import models
from django.contrib.auth.models import User
class Topic(models.Model):
text = models.CharField(max_length=100)
def __str__(self):
return self.text
class Description(models.Model):
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = 'Descriptions'
def __str__(self):
if len(self.text) > 100:
return f'{self.text[:100]}...'
else:
return self.text
CodePudding user response:
The reason you are getting this error is that you are not associating any topic to the newly created Description
. Let's give it a try
# ...
form = DescriptionForm(data=request.POST)
if form.is_valid():
new_description = form.save(commit=False)
new_description.owner = request.user
new_description.topic = topic # new
new_description.save()
# ....
CodePudding user response:
In your code, please set new_description.topic
, in def edit_description
, so:
@login_required
def new_description(request, topic_id):
topic = Topic.objects.get(id=topic_id)
if request.method != 'POST':
form = DescriptionForm()
else:
form = DescriptionForm(data=request.POST)
if form.is_valid():
# >>>> SET TOPIC HERE <<<<<<<
new_description.topic = topic
new_description = form.save(commit=False)
new_description.topic = topic
new_description.owner = request.user
new_description.save()
return redirect('description', topic_id=topic_id)
context = {'topic': topic, 'form': form}
return render(request, 'Menu/new_description.html', context)