# NOT NULL constraint failed

**URL:** <https://forum.djangoproject.com/t/not-null-constraint-failed/9460>\
**Category:** Forms & APIs\
**Created:** [August 29, 2021, 7:44pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460 "2021-08-29T19:44:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 7:44pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/1 "2021-08-29T19:44:51Z")

</div>

Hey,

I created a Job Model with a ModelForm to create the Job. If I click the submit button at the form this error messages appears:

```auto
Internal Server Error: /createjob/
Traceback (most recent call last):
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: Site_job.job_creator_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Finn\PyCharmProjects\JobMaze\JobMaze\Site\views.py", line 77, in job_create
    form.save()
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\models.py", line 468, in save
    self.instance.save()
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
    updated = self._save_table(
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
    return manager._insert(
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: Site_job.job_creator_id

```

I guess something is wrong with the job\_creator Field in my Models.py. I’ve used the normal user from the Auth Library for my ForeignKey.

Models.py

```auto
from django.db import models
from django.contrib.auth.models import User

class Job(models.Model):
    job_name = models.CharField(max_length=50)
    job_description = models.CharField(max_length=200)
    job_creator = models.ForeignKey(User, on_delete=models.CASCADE)
    job_price = models.IntegerField()

```

Forms.py

```auto
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.forms import ModelForm

from .models import Job

class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

class JobForm(ModelForm):
    class Meta:
        model = Job
        fields = ['job_name', 'job_description', 'job_price']

```

views.py

```auto
def job_create(request):
    if request.method == 'POST':
        form = JobForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home:adjust')
    else:
        form = JobForm()
    return render(request,
                  'home/job_create.html',
                  {'form': form}

                  )

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 29, 2021, 7:53pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/2 "2021-08-29T19:53:25Z")

</div>

> [@FLYAX](#):
>
> ```auto
> def job_create(request):
> if request.method == 'POST':
> form = JobForm(request.POST)
> if form.is_valid():
> form.save()
> return redirect('home:adjust')
> 
> ```

The `job_creator` field is not in your `JobForm`, nor are you assigning a value to it in your view.  
Since you’re not providing a value for it, it’s null. Your model definition for that field does not allow nulls, so it’s throwing the error.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 7:56pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/3 "2021-08-29T19:56:42Z")

</div>

yeah that part with the creator in the form should be like this because the person who fills the form is always the creator so I removed the job\_creator field from the form. What should I change in the views and in the Model Field that the error doesnt appear?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 29, 2021, 8:30pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/4 "2021-08-29T20:30:46Z")

</div>

You’ll want to set a value on that field.

(In any view, how do you know who the user is making a request?)

Also see the docs on [the save() method](https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#the-save-method), particularly the text after the first example block and the second example.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 9:13pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/5 "2021-08-29T21:13:14Z")

</div>

and how can I set a value on that field, like automaticly by submitting the form not as a seperate field on the form. The user which is submitting should be the job\_creator.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 9:25pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/6 "2021-08-29T21:25:28Z")

</div>

I tried to set the job creator manually in the views:

```auto
def job_create(request):

    if request.method == 'POST':

        form = JobForm(request.POST)

        if form.is_valid():

            Job.job_creator = request.user

            form.save()

            return redirect('home:adjust')

    else:

        form = JobForm()

    return render(request,

                  'home/job_create.html',

                  {'form': form}

                  )

```

But the NOT NULL Error things still shows up

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 29, 2021, 9:44pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/7 "2021-08-29T21:44:55Z")

</div>

You are really close - you need to save the form first (using the commit=False parameter). This call returns an instance of the object. Then you can assign request.user to the field on the object and then call save on the object. (Basically the process in the second example as referenced above, with the exception that there’s no many-to-many field involved.)

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 11:02pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/8 "2021-08-29T23:02:08Z")

</div>

So I’ve addet the commit=false Parameter and it worked. And I saved my Object with Job.save()

```auto
def job_create(request):
    if request.method == 'POST':
        form = JobForm(request.POST)
        if form.is_valid():
            Job.job_creator = request.user
            form.save(commit=False)
            Job.save
            return redirect('home:adjust')
    else:
        form = JobForm()
    return render(request,
                  'home/job_create.html',
                  {'form': form}

                  )

```

Is this how you meant?

And another question how can I see all Job Model Instances I’ve created is there an command or a list to look that up?

And thanks for your help again!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 29, 2021, 11:20pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/9 "2021-08-29T23:20:11Z")

</div>

> [@FLYAX](#):
>
> Is this how you meant?

Please post the code here from the second example in the docs on [the save() method](https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#the-save-method).

> [@FLYAX](#):
>
> And another question how can I see all Job Model Instances I’ve created is there an command or a list to look that up?

Are you talking about creating a page with such a list or something else? (You also have the Django [shell](https://docs.djangoproject.com/en/3.2/ref/django-admin/#shell) and [dumpdata](https://docs.djangoproject.com/en/3.2/ref/django-admin/#dumpdata) commands that you could use.)

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 11:30pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/10 "2021-08-29T23:30:18Z")

</div>

Ahh you mean the many to many save type.

```auto
def job_create(request):

    if request.method == 'POST':

        form = JobForm(request.POST)

        if form.is_valid():

            Job.job_creator = request.user

            form.save(commit=False)

            form.save_m2m()

            return redirect('home:adjust')

    else:

        form = JobForm()

    return render(request,

                  'home/job_create.html',

                  {'form': form}

                  )

```

And:

No I’m not trying to make a site with them I just want to see how much and which ones I have, I’m going to look at the commands you’ve send me, maybe there is smthng like I search

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 29, 2021, 11:32pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/11 "2021-08-29T23:32:24Z")

</div>

From my earlier response:

Basically the process in the second example as referenced above, **with the exception that there’s no many-to-many field involved**. (Emphasis added)

So please post that example, not your code.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 29, 2021, 11:45pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/12 "2021-08-29T23:45:48Z")

</div>

# Create a form instance with POST data.

> > > f = AuthorForm(request.POST)

# Create, but don’t save the new author instance.

> > > new\_author = f.save(commit=False)

# Modify the author in some way.

> > > new\_author.some\_field = ‘some\_value’

# Save the new instance.

> > > new\_author.save()

# Now, save the many-to-many data for the form.

> > > f.save\_m2m()

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 29, 2021, 11:50pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/13 "2021-08-29T23:50:48Z")

</div>

```auto
# Create a form instance with POST data.
>>> f = AuthorForm(request.POST)

# Create, but don't save the new author instance.
>>> new_author = f.save(commit=False)

# Modify the author in some way.
>>> new_author.some_field = 'some_value'

# Save the new instance.
>>> new_author.save()

```

So _this_ is the common pattern.

1. The form is bound to the posted data
2. The form is saved with commit=False, the return value is an instance of the Model.
3. The model field has a field set to some value not from the form.
4. The modified model is saved.

These steps, in this order.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 30, 2021, 2:14pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/14 "2021-08-30T14:14:05Z")

</div>

Like this? I’ve done it like in your pattern.

```auto
def job_create(request):
    if request.method == 'POST':
        form = JobForm(request.POST)
        if form.is_valid():
            Job = form.save(commit=False)
            Job.job_creator = request.user
            Job.save()

            return redirect('home:adjust')
    else:
        form = JobForm()
    return render(request,
                  'home/job_create.html',
                  {'form': form}

                  )

```

But the Job=form.save… thing is another like in my Model how does the code know that the Job Variable belongs to the Model

And btw this code works I logged myself in the DB and there is a list of all jobs I’ve created

![grafik](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/3/32b425b92ad7e9cf31da4b4bc3805f02ce5faeae.png)

You can see the job\_creator id on job 3 is another because I used another account there, so it works as it should right?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 30, 2021, 2:21pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/15 "2021-08-30T14:21:09Z")

</div>

Yep, the pattern is correct.

What confused me the first time you posted this was the reuse of “Job” as the name of an instance of an object. You _really_ want to pick a different name because you don’t want to confuse your use of “Job” the variable with “Job” the Model class, and changing what Job is referring to can cause problems.

> [@FLYAX](#):
>
> how does the code know that the Job Variable belongs to the Model

Because you define it as such:

> [@FLYAX](#):
>
> `Job = form.save(commit=False)`

The save method returns the instance of the model created by that form.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 30, 2021, 2:28pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/16 "2021-08-30T14:28:53Z")

</div>

So I’ve now renamed the Model to “JobModel” that I dont confound both.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 30, 2021, 2:30pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/17 "2021-08-30T14:30:23Z")

</div>

You still ought to conform with the standard Python conventions.

At a minimum, you want it to be `job = form.save(...)` and _not_ `Job = form.save(...)`. What you get back from the save method is an instance, not a class.

---

<div class="post-metadata">

**Author:** ![FLYAX](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/flyax/32/3422_2.png) [@FLYAX](https://forum.djangoproject.com/u/FLYAX)\
**Post date:** [August 30, 2021, 2:33pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/18 "2021-08-30T14:33:50Z")

</div>

Yup,  
Changed it! I have to read the Python conventions never did that tbh because I came from web development to Python.

---

<div class="post-metadata">

**Author:** ![Chief240](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chief240/32/9802_2.png) [@Chief240](https://forum.djangoproject.com/u/Chief240)\
**Post date:** [August 23, 2022, 2:51pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/19 "2022-08-23T14:51:42Z")

</div>

I have the same issues too, have no idea how to fix

 ![trans](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/c/cdb040c1fd8d16ac955c9d54394b0335097bcb3e.jpeg)

---

<div class="post-metadata">

**Author:** ![Chief240](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chief240/32/9802_2.png) [@Chief240](https://forum.djangoproject.com/u/Chief240)\
**Post date:** [August 23, 2022, 2:54pm UTC](https://forum.djangoproject.com/t/not-null-constraint-failed/9460/20 "2022-08-23T14:54:39Z")

</div>

![Capture11111111](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/13fc8468b9064f0068f83d1616a2119ff444e400.jpeg)

[Next page](https://forum.djangoproject.com/t/not-null-constraint-failed/9460.md?page=2)
