Changes between Initial Version and Version 1 of Ticket #34401
- Timestamp:
- Mar 10, 2023, 8:32:26 AM (21 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #34401 – Description
initial v1 1 1 Since 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 2 2 and in particular those 2 lines in ` django/contrib/contenttypes/fields.py`: 3 ``` 3 4 {{{ 4 5 if rel_obj is None and self.is_cached(instance): 5 6 return rel_obj 6 ``` 7 }}} 7 8 8 9 With those 2 lines removed, the bug doesn't reproduce. I'm unsure if this is an acceptable solution for Django though. … … 11 12 12 13 models.py: 13 ``` 14 {{{ 14 15 from django.contrib.contenttypes.fields import GenericForeignKey 15 16 from django.contrib.contenttypes.models import ContentType … … 40 41 matching_obj.associated_to = target 41 42 matching_obj.save() 42 ``` 43 }}} 43 44 44 45 tests.py: 45 ``` 46 47 {{{ 46 48 from django.test import TestCase 47 49 from .models import * … … 66 68 self.first_or_second.refresh_from_db() 67 69 self.assertEqual(self.first_or_second.associated_to, self.first) 68 ``` 70 }}} 69 71 70 72 The last assertion fails because of this bug.