I am creating a ticket app in my django project. When I try to create a ticket the NOT NULL constraint failed: tickets_ticket.name
error shows up. I’m not sure why the value for ticket.name
field won’t pass correctly. How do I proceed? any help is much appreciated.
Here’s what i have so far
models.py
class Category(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Ticket(models.Model):
STATUS = (
(True, 'Open'),
(False, 'Closed')
)
PRIORITIES = (
('None', 'None'),
('Low', 'Low'),
('Medium', 'Medium'),
('High', 'High')
)
TYPE = (
('Misc', 'Misc'),
('Bug', 'Bug'),
('Help Needed', 'Help Needed'),
('Concern', 'Concern'),
('Question', 'Question')
)
host = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True, related_name='host')
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='category')
name = models.CharField(max_length=200, null=True)
status = models.BooleanField(choices=STATUS, default=True)
priority = models.TextField(choices=PRIORITIES, default='None', max_length=10)
type = models.TextField(choices=TYPE, default='Misc', max_length=15)
description = RichTextField(null=True, blank=True)
# description = models.TextField(null=True, blank=True)
participants = models.ManyToManyField(CustomUser, related_name='participants', blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-updated', '-created']
def __str__(self):
return self.name
views.py
view for creating a ticket
def createTicket(request):
form = TicketForm()
categories = Category.objects.all()
if request.method == 'POST':
category_name = request.POST.get('category')
category, created = Category.objects.get_or_create(name=category_name)
ticket = Ticket.objects.create(
host = request.user,
category=category,
name=request.POST.get('name'),
status=request.POST.get('status'),
priority=request.POST.get('priority'),
type=request.POST.get('type'),
description=request.POST.get('description'),
)
return redirect('ticket', pk=ticket.id)
context = {'form': form, 'categories': categories}
return render(request, 'projects/ticket_form.html', context)
view for ticket page
def ticket(request, pk):
ticket = Ticket.objects.get(id=pk)
ticket_messages = ticket.message_set.all()
participants = ticket.participants.all()
if request.method == 'POST':
message = Message.objects.create(
user=request.user,
ticket=ticket,
body=request.POST.get('body')
)
ticket.participants.add(request.user)
return redirect('ticket', pk=ticket.id)
context = {'ticket': ticket, 'ticket_messages': ticket_messages,
'participants': participants}
return render(request, 'projects/ticket.html', context)
here is part of the template for the ticket page
<div class="main-column">
<form action="" method="POST">
{% csrf_token %}
<div class="form__group">
<label for="ticket_name">Ticket Name</label>
{{form.name}}
</div>
<div class="form__group">
<label for="ticket_description">Ticket Description</label>
{{form.description}}
</div>
</form>
</div>
ticket form
class TicketForm(ModelForm):
description = forms.CharField(widget = CKEditorWidget())
class Meta:
model = Ticket
fields = '__all__'
exclude = ['host', 'participants']