Changes between Initial Version and Version 1 of Ticket #34401


Ignore:
Timestamp:
Mar 10, 2023, 8:32:26 AM (20 months ago)
Author:
François Dupayrat
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34401 – Description

    initial v1  
    11Since Django 4, `refresh_from_db`exhibit an inconsistent behavior for `GenericForeignKey` fields: sometime, the object field is properly refreshed, and sometime it isn't. That issue has been introduced with Django 4 and I've traced it back to this ticket: https://code.djangoproject.com/ticket/33008
    22and in particular those 2 lines in ` django/contrib/contenttypes/fields.py`:
    3 ```
     3
     4{{{
    45        if rel_obj is None and self.is_cached(instance):
    56            return rel_obj
    6 ```
     7}}}
    78
    89With those 2 lines removed, the bug doesn't reproduce. I'm unsure if this is an acceptable solution for Django though.
     
    1112
    1213models.py:
    13 ```
     14{{{
    1415from django.contrib.contenttypes.fields import GenericForeignKey
    1516from django.contrib.contenttypes.models import ContentType
     
    4041        matching_obj.associated_to = target
    4142        matching_obj.save()
    42 ```
     43}}}
    4344
    4445tests.py:
    45 ```
     46
     47{{{
    4648from django.test import TestCase
    4749from .models import *
     
    6668        self.first_or_second.refresh_from_db()
    6769        self.assertEqual(self.first_or_second.associated_to, self.first)
    68 ```
     70}}}
    6971
    7072The last assertion fails because of this bug.
Back to Top