﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34084	ModelForms always set self.instance even when none passed in	Steven Mapes	nobody	"I came across this bug this morning when running my testsuite against Django 3.2, 4.0 and 4.1 (4.1.0, 4.1.1 and 4.1.2).  The issue is with ModelForms when they are used without passing in an existing saved instance. I thought this was related to the regression that was then fixed in 4.1.2 and mentioned in both [https://code.djangoproject.com/ticket/33984 33984] and [https://code.djangoproject.com/ticket/33952 33952] but the issue is still occurring even though both of those are closed.

To test you can use the below models, form and unit test

{{{
# models.py
from django.db import models


class Tag(models.Model):
    tag = models.SlugField(max_length=64, unique=True)


class Thing(models.Model):
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE, related_name=""things"")
    active = models.BooleanField(default=True)

}}}

{{{
# forms.py
from django import forms
from example.models import Tag


class TagForm(forms.ModelForm):
    class Meta:
        model = Tag
        fields = [""tag""]

    def __init__(self, *args, **kwargs):
        super(TagForm, self).__init__(*args, **kwargs)

        if self.instance and self.instance.things:
            inactive_things = self.instance.things.filter(active=False)
            # Do something with it

}}}


{{{
from unittest.case import TestCase
from example.forms import TagForm


class TagFormCase(TestCase):
    def test_required(self):
        """"""Test required fields""""""
        form = TagForm({})
        self.assertFalse(form.is_valid())
        self.assertEqual(
            {
                ""tag"": [""This field is required.""],
            },
            form.errors,
        )


}}}

If you run the test on Django versions <4.1 including Django 2.*, Django 3.* and 4.0.* then it'll pass but on 4.1.* it'll fail with ```ValueError: 'Tag' instance needs to have a primary key value before this relationship can be used.```

In order to pass the if statement needs to be changed to ```if self.instance.pk and self.instance.things``` as self.instance is no longer None if no instance is passed in.

If your model uses a custom primary key such as a uuid4 then it'll work as expected as the uuid4 is already called
"	Bug	closed	Forms	4.1	Normal	invalid	modelform	David Kwong	Unreviewed	0	0	0	0	0	0
