diff options
| author | Ben Cail <bcail@crossway.org> | 2023-11-15 14:32:03 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-05 08:25:23 +0100 |
| commit | 02a600ff67f7b106cdcab22310bacea98c1a26ba (patch) | |
| tree | 5806f49c4417747082eceed3f84422c0f3203016 /django | |
| parent | a47de0d6cd440d4515ede48df8335d91d7ac7793 (diff) | |
Fixed #16281 -- Fixed ContentType.get_object_for_this_type() in a multiple database setup.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 4 | ||||
| -rw-r--r-- | django/contrib/contenttypes/models.py | 6 |
2 files changed, 6 insertions, 4 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 1b6abb9818..ce731bf2dd 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -280,7 +280,9 @@ class GenericForeignKey(FieldCacheMixin): if ct_id is not None: ct = self.get_content_type(id=ct_id, using=instance._state.db) try: - rel_obj = ct.get_object_for_this_type(pk=pk_val) + rel_obj = ct.get_object_for_this_type( + using=instance._state.db, pk=pk_val + ) except ObjectDoesNotExist: pass self.set_cached_value(instance, rel_obj) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 0d98ed3a4d..4f16e6eb69 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -174,20 +174,20 @@ class ContentType(models.Model): except LookupError: return None - def get_object_for_this_type(self, **kwargs): + def get_object_for_this_type(self, using=None, **kwargs): """ Return an object of this type for the keyword arguments given. Basically, this is a proxy around this object_type's get_object() model method. The ObjectNotExist exception, if thrown, will not be caught, so code that calls this method should catch it. """ - return self.model_class()._base_manager.using(self._state.db).get(**kwargs) + return self.model_class()._base_manager.using(using).get(**kwargs) def get_all_objects_for_this_type(self, **kwargs): """ Return all objects of this type for the keyword arguments given. """ - return self.model_class()._base_manager.using(self._state.db).filter(**kwargs) + return self.model_class()._base_manager.filter(**kwargs) def natural_key(self): return (self.app_label, self.model) |
