Changes between Initial Version and Version 2 of Ticket #34816


Ignore:
Timestamp:
Sep 7, 2023, 12:25:40 AM (13 months ago)
Author:
Richard Laager
Comment:

Sorry, that's just a red herring from writing up the bug report. Here is a real example from right now:

In [1]: from case.models import Case

In [2]: case = Case.objects.get(id=REDACTED)

In [3]: case.content_object
Out[3]: <REDACTED: REDACTED>

In [4]: case.content_type_id = 175

In [5]: case.object_id = '218-555-1212'

In [6]: case.content_object
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /srv/www/testing/lib/.venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py:1836, in IntegerField.to_python(self, value)
   1835 try:
-> 1836     return int(value)
   1837 except (TypeError, ValueError):

ValueError: invalid literal for int() with base 10: '218-555-1212'

During handling of the above exception, another exception occurred:

ValidationError                           Traceback (most recent call last)
Cell In[6], line 1
----> 1 case.content_object

File /srv/www/testing/lib/.venv/lib/python3.10/site-packages/django/contrib/contenttypes/fields.py:233, in GenericForeignKey.__get__(self, instance, cls)
    231 if rel_obj is not None:
    232     ct_match = ct_id == self.get_content_type(obj=rel_obj, using=instance._state.db).id
--> 233     pk_match = rel_obj._meta.pk.to_python(pk_val) == rel_obj.pk
    234     if ct_match and pk_match:
    235         return rel_obj

File /srv/www/testing/lib/.venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py:1838, in IntegerField.to_python(self, value)
   1836     return int(value)
   1837 except (TypeError, ValueError):
-> 1838     raise exceptions.ValidationError(
   1839         self.error_messages['invalid'],
   1840         code='invalid',
   1841         params={'value': value},
   1842     )

ValidationError: ['“218-555-1212” value must be an integer.']

If step 3 (accessing case.content_object) is omitted, the problem does not occur because there is no cached content_object. Alternatively, if between 3 and 4 one does case.content_object = None to clear the cache, the problem does not occur.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34816

    • Property Has patch unset
  • Ticket #34816 – Description

    initial v2  
    553. Change the instance of "A" to reference an instance of model "C" with a PK that is incompatible with int(). But make this change using `content_type_id` and `object_id` properties, not `content_object`, i.e.:
    66{{{#!python
    7     a.content_type_id = ContentType.objects.get_for_model(B)
     7    a.content_type_id = ContentType.objects.get_for_model(B).pk
    88    a.object_id = "foo"
    99}}}
Back to Top