With an upgrade from Django 4.1 to 4.2 (but also verified in Django 5.0),
I've noticed a change in behavior with how on_delete=models.SET is
handled.
Given the following models:
{{{
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=32)
def __str__(self):
return self.name
def get_default_person():
return Person.objects.get_or_create(name="ghost")[0]
class Pet(models.Model):
name = models.CharField(max_length=32)
person = models.ForeignKey(Person, related_name="pets",
on_delete=models.SET(get_default_person))
def __str__(self):
return self.name
}}}
I can see what follows in Django 4.2+ (in ./manage.py shell):
{{{
>>> from pets.models import Person, Pet
>>> Person.objects.all()
<QuerySet []>
>>> Pet.objects.all()
<QuerySet []>
>>> Person.objects.create(name="johndoe")
<Person: johndoe>
>>> Person.objects.all()
<QuerySet [<Person: johndoe>]>
>>> Person.objects.all().delete()
(1, {'pets.Person': 1})
>>> Person.objects.all()
<QuerySet [<Person: ghost>]>
}}}
What is strange to me is that the "ghost" Person instance is created upon
deletion of the "johndoe" instance, even if there are no Pets with a
ForeignKey to "johndoe".
Django 4.1 behaves differently (no "ghost" Person is created on deletion
of other Person objects).
Is this an intended change? I couldn't find any documentation of this in
the release notes.
Thanks so much for your help.
Fabio
--
Ticket URL: <https://code.djangoproject.com/ticket/35073>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => O'ktamjon
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/35073#comment:1>