Django: Exclude field if select_related() gets used

662 Views Asked by At

I have a Job and Blob model like this:

class Job(models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    start_time = models.DateTimeField()
    input = models.ForeignKey('Blob')


class DeferContentManager(models.Manager):
    use_for_related_fields = True

    def get_queryset(self, *args, **kwargs):
        return super(DeferContentManager, self).get_queryset(*args, **kwargs).defer('content')

class Blob(models.Model):
    content = models.BinaryField()
    name = models.CharField(max_length=10000, default='')


    objects = DeferContentManager()

Unfortunately there is an Django ORM query which does this:

Job.objects.filter(....).select_related()

This loads the BinaryFields of the jobs and we get a MemoryError.

Is there a way to exclude the BinaryField content if I do a select_related() query like above?

PS: I know that I could alter the query and use select_related('non_binary_field') but a general solution is needed here. I do not have access to the code which does call this line: Job.objects.filter(....).select_related()

1

There are 1 best solutions below

0
On

You should define base_manager_name in Meta.

use_for_related_fields = True was deprecated with 1.10 and removed in 2.0

See: https://docs.djangoproject.com/en/1.11/ref/models/options/#base-manager-name