# ModelForm.save() raises IntegrityError

**URL:** <https://forum.djangoproject.com/t/modelform-save-raises-integrityerror/4488>\
**Category:** Using Django\
**Created:** [September 24, 2020, 4:33am UTC](https://forum.djangoproject.com/t/modelform-save-raises-integrityerror/4488 "2020-09-24T04:33:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenLHedgepeth](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/benlhedgepeth/32/2225_2.png) [@BenLHedgepeth](https://forum.djangoproject.com/u/BenLHedgepeth)\
**Post date:** [September 24, 2020, 4:33am UTC](https://forum.djangoproject.com/t/modelform-save-raises-integrityerror/4488/1 "2020-09-24T04:33:50Z")

</div>

Within a TestCase, I’m attempting to create a model instance as the result of calling `.save()` on a _ModelForm_ within _TestCase.setUpTestData_. However I get the following error: `django.db.utils.IntegrityError: NOT NULL constraint failed: photos_photo.photographer_id`. As shown in the test a _User_ is attached, so I’m not sure why this error is still being raised?

_test\_forms.py_

```auto
class RedundantImageUpload(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.test_image = SimpleUploadedFile(
            "test_image.jpg",
            content=b'''GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00
            \x00ccc,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00''',
            content_type="image/gif"
        )
        user = User.objects.create_user("User")

        form = PhotoForm({'title': "Image Title"}, {'source': cls.test_image})
        form.save(commit=False)
        form.photographer = user
        form.save()

        cls.submitted_form = PhotoForm(
            {"title": "Image Title"},
            {"source": cls.test_image}
        )

    def test_image_upload_path_exists(self):
        with self.assertRaisesMessage(ValidationError, "Image already uploaded: test_image.jpg"):
            self.submitted_form.errors

```

_forms.py_

```auto
from django import forms
from django.core.exceptions import ValidationError
from django.core.files.storage import get_storage_class

from .models import Photo

def validate_title(value):
    title = value.strip()
    if not title:
        raise ValidationError("Must provide image title", code="blank_image_title")

class PhotoForm(forms.ModelForm):
    title = forms.CharField(strip=False, validators=[validate_title])

    def clean_source(self):
        stored_user_uploads = get_storage_class()()
        file_name = self.cleaned_data["source"].name
        if stored_user_uploads.exists(file_name):
            raise ValidationError(f"Image already uploaded: {file_name}")
        return self.cleaned_data["source"]

    class Meta:
        model = Photo
        fields = ["source", "title"]

```

_models.py_

```auto
from django.db import models
from django.conf import settings

# Create your models here.
class Photo(models.Model):
    source = models.ImageField(upload_to='uploads/%Y/%m/%d/')
    title = models.CharField(max_length=50)
    upload_date = models.DateField(auto_now_add=True)
    likes = models.IntegerField(default=0)
    photographer = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )

    def __str__ (self):
        return self.title

```

---

<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:** [September 24, 2020, 4:46am UTC](https://forum.djangoproject.com/t/modelform-save-raises-integrityerror/4488/2 "2020-09-24T04:46:51Z")

</div>

> [@BenLHedgepeth](#):
>
> ```auto
> form = PhotoForm({'title': "Image Title"}, {'source': cls.test_image})
> form.save(commit=False)
> form.photographer = user
> form.save()
> 
> ```

Not quite - when you issue the [form.save(commit=False)](https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/#the-save-method), the return value from that method is the instance of the object being created from the form.

You’re looking to do something closer to this:

```auto
        form = PhotoForm({'title': "Image Title"}, {'source': cls.test_image})
        photo = form.save(commit=False)
        photo.photographer = user
        photo.save()

```
