#35073: New behavior of ForeignKey with on_delete=models.SET (Django 4.2 and
5.0)
-------------------------------------+-------------------------------------
Reporter: Fabio | Owner: nobody
Sangiovanni |
Type: | Status: new
Uncategorized |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hello everybody.
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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018cbbc40b9d-aceb5aa3-8755-449b-a153-06f97bbb829e-000000%40eu-central-1.amazonses.com.