# ORM query to prefetch from different databases without relationship field

**URL:** <https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318>\
**Category:** Using the ORM\
**Created:** [February 23, 2022, 8:08pm UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318 "2022-02-23T20:08:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KStenK](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kstenk/32/7581_2.png) [@KStenK](https://forum.djangoproject.com/u/KStenK)\
**Post date:** [February 23, 2022, 8:08pm UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318/1 "2022-02-23T20:08:19Z")

</div>

Hi there!

Currently, I have two Models which are in different databases and I need to make a query from both databases to show related data on the list view.

Example models:

```auto
class A(models.Model):
    b_uuid = models.UUIDField(db_index=True)

class B(models.Model):
    uuid = models.UUIDField(unique=True, editable=False, db_index=True)

    name = models.CharField(max_length=40)

    class Meta:
        managed = False

```

Everything works fine when I’m using models to query databases separately.

Is there any way I can use Django ORM by fetching data with the **A** model and then prefetching all **B** model objects, where **A** model objects **b\_uuid** field is related with **B** model **uuid** field inside the **A** model queryset?

And the data will be set to **A** model queryset objects attribute.

Same as prefetching between two models with a Foreign Key relationship, but not using the Foreign Key field.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 23, 2022, 9:40pm UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318/2 "2022-02-23T21:40:56Z")

</div>

You can do it manually the same way that Django effectively does a prefetch\_related.

Get a list of all the `A` `b_uuid` fields, then execute a filter on `B` where `uuid__in` that list, then match up the results between the two in your code.

---

<div class="post-metadata">

**Author:** ![KStenK](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kstenk/32/7581_2.png) [@KStenK](https://forum.djangoproject.com/u/KStenK)\
**Post date:** [February 24, 2022, 8:35am UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318/3 "2022-02-24T08:35:47Z")

</div>

Thank you for your answer.

In the end, that’s what I thought I will do if there is no other way to do it natively with ORM. 🙂

---

<div class="post-metadata">

**Author:** ![DanielGnzlzVll](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danielgnzlzvll/32/4585_2.png) [@DanielGnzlzVll](https://forum.djangoproject.com/u/DanielGnzlzVll)\
**Post date:** [February 24, 2022, 5:49pm UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318/4 "2022-02-24T17:49:45Z")

</div>

I think this could help you [QuerySet API reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/querysets/#prefetch-related-objects)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 24, 2022, 11:13pm UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318/5 "2022-02-24T23:13:26Z")

</div>

Actually that’s not going to help here. Django does not recognize relationships across different databases.

---

<div class="post-metadata">

**Author:** ![DanielGnzlzVll](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danielgnzlzvll/32/4585_2.png) [@DanielGnzlzVll](https://forum.djangoproject.com/u/DanielGnzlzVll)\
**Post date:** [February 25, 2022, 12:46am UTC](https://forum.djangoproject.com/t/orm-query-to-prefetch-from-different-databases-without-relationship-field/12318/6 "2022-02-25T00:46:48Z")

</div>

I’ve successfully gotten it to work.

`settings.py`

```python
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    },
    "replica": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "dbreplica.sqlite3",
    }
}

DATABASE_ROUTERS = ['myapp.routers.TestRouter',]

```

`myapp.models.py`

```python
from django.db import models

class BManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().using('replica')

class A(models.Model):
    name = models.CharField(max_length=100)

class B(models.Model):
    name = models.CharField(max_length=100)
    a = models.ForeignKey(A, on_delete=models.DO_NOTHING, db_constraint=False)
    objects = BManager()

```

`myapp.test.py`

```python
import pytest
from django.db.models import prefetch_related_objects
from model_bakery import baker

from myapp import models

@pytest.mark.django_db(databases=['default', 'replica'])
def test_multidb_prefetch():
    
    a_list = [baker.make('A') for i in range(10)]
    for a in a_list:
        baker.make(
            'B',
            a=a,
            _using='replica',
            _quantity=3,
        )

    a_list = models.A.objects.all()
    
    prefetch_related_objects(a_list, 'b_set')
    assert a_list[0].b_set.all().count() == 3

```
