pythondjangodjango-formsmodelform

Django ModelForm doesn't update instance but fails with IntegrityError


I'm having some really weird issue with a ModelForm. Instead of saving the instance, it attempts to create the instance with the same primary key.

class Upload(models.Model):
    file = models.FileField(upload_to=get_foundry_upload_name, null=False, blank=False)
    filename = models.CharField(max_length=256, null=True, blank=True)

    foundry = models.ForeignKey(Foundry, on_delete=models.CASCADE)

    family = models.CharField(max_length=256, null=True, blank=True)
    family_select = models.ForeignKey(Family, null=True, blank=True, on_delete=models.CASCADE)

    style = models.CharField(max_length=256, null=True, blank=True)
    style_select = models.ForeignKey(Style, null=True, blank=True, on_delete=models.CASCADE)

    processed = models.BooleanField(default=False)
    created = models.DateTimeField("Created", auto_now_add=True)

    # edit: this was omitted in the original question, because I thought it irrelevant
    def save(self, commit=True):
        self.filename = self.file.name
        super().save(commit)


class UploadProcessForm(ModelForm):
    class Meta:
        model = Upload
        fields = (
            "filename",
            "family",
            "family_select",
            "style",
            "style_select",
        )


def upload_process_row(request, foundry, id):
    
    i = get_object_or_404(Upload, id=id, foundry=foundry)
    upload_form = UploadProcessForm(instance=i)

    if request.method == "POST":
        upload_form = UploadProcessForm(request.POST, instance=i)
        if upload_form.is_valid():
            upload_form.save()
    
    return render(request, "foundry/upload_process.row.html", {
        "f": upload_form
    })

django.db.utils.IntegrityError: duplicate key value violates unique constraint "foundry_upload_pkey" DETAIL: Key (id)=(1) already exists.

I'm certain this is some super trivial mistake, but I just cannot spot where I'm going wrong; imo this looks exactly like the textbook example. The upload_form.save() always attempts to create a database entry, and with the instance's primary key. I'd just want to update the existing instance (that's the whole point of a ModelForm, no?).

I've wiped the database table and migrations and recreated them fresh, just to be sure.

Edit: I've added the save overwrite in Upload that I originally omitted as irrelevant. A closer look at the stacktrace (:facepalm:) pointed out that it is that save() call where the pk error originates from. However, now that I know where the error is triggered, I still don't understand why? Shouldn't that simply save with the same pk again?


Solution

  • The model's save method used an wrong/outdated signature. It should be:

    def save(self, **kwargs):
            self.filename = self.file.name
            super().save(**kwargs)
    

    The original triggered the the commit when it should not, thus resulting in duplicate primary keys.