NOT NULL constraint failed:

I am creating a ticket app in my django project. When I try to create a ticket the NOT NULL constraint failed: error shows up. I’m not sure why the value for field won’t pass correctly. How do I proceed? any help is much appreciated.

Here’s what i have so far

class Category(models.Model):
    name = models.CharField(max_length=200, null=True)
    def __str__(self):
class Ticket(models.Model):
    STATUS = (
        (True, 'Open'),
        (False, 'Closed')
        ('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):

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,
        return redirect('ticket',
    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(
        return redirect('ticket',
    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>
              <div class="form__group">
                <label for="ticket_description">Ticket Description</label>

ticket form

class TicketForm(ModelForm):
    description = forms.CharField(widget = CKEditorWidget())
    class Meta:
        model = Ticket
        fields = '__all__'
        exclude = ['host', 'participants']

You’re using a model form, so you should take advantage of it.

You’re also not checking for any errors in the form - you have no way of knowing if something was improperly submitted.

In general, this:

Should more appropriately look something like this:

def createTicket(request):
    form = TicketForm(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            # You're not showing how you're rendering 'category' in your template
            # This may/may not be the best way to handle this.
            category_name = request.POST.get('category')
            category, created = Category.objects.get_or_create(name=category_name)

            ticket =
   = request.user
            ticket.category = category
            return redirect('ticket',
            # Do something here to identify the fact that you have an invalid form
            # Like rerender the same form as with the get.     
        # Render the form

Also, review the sample at Working with forms | Django documentation | Django

1 Like