Search code examples
pythondjangodjango-querysetdjango-select-related

Is there a way to check whether a related object is already fetched?


I would like to be able to check if a related object has already been fetched by using either select_related or prefetch_related, so that I can serialize the data accordingly. Here is an example:

class Address(models.Model):
    street = models.CharField(max_length=100)
    zip = models.CharField(max_length=10)

class Person(models.Model):
    name = models.CharField(max_length=20)
    address = models.ForeignKey(Address)

def serialize_address(address):
    return {
        "id": address.id,
        "street": address.street,
        "zip": address.zip
    }

def serialize_person(person):
    result = {
        "id": person.id,
        "name": person.name
    }
    if is_fetched(person.address):
        result["address"] = serialize_address(person.address)
    else:
        result["address"] = None

######

person_a = Person.objects.select_related("address").get(id=1)
person_b = Person.objects.get(id=2)

serialize_person(person_a) #should be object with id, name and address
serialize_person(person_b) #should be object with only id and name

In this example, the function is_fetched is what I am looking for. I would like to determine if the person object already has a resolves address and only if it has, it should be serialized as well. But if it doesn't, no further database query should be executed.

So is there a way to achieve this in Django?


Solution

  • If the address relation has been fetched, then the Person object will have a populated attribute called _address_cache; you can check this.

    def is_fetched(obj, relation_name):
        cache_name = '_{}_cache'.format(relation_name)
        return getattr(obj, cache_name, False)
    

    Note you'd need to call this with the object and the name of the relation:

    is_fetched(person, 'address')
    

    since doing person.address would trigger the fetch immediately.

    Edit reverse or many-to-many relations can only be fetched by prefetch_related; that populates a single attribute, _prefetched_objects_cache, which is a dict of lists where the key is the name of the related model. Eg if you do:

    addresses = Address.objects.prefetch_related('person_set')
    

    then each item in addresses will have a _prefetched_objects_cache dict containing a "person' key.

    Note, both of these are single-underscore attributes which means they are part of the private API; you're free to use them, but Django is also free to change them in future releases.