10

In my Django app I want to use select_related() on a QuerySet to "follow" a ForeignKey field, but I only need to access a few of the fields on the "followed" model instance. Can I use the defer() method somehow with my "followed" field.

e.g., if I have...

class BarModel(models.Model):
    ...
    blah = models.TextField()

class FooModel(models.Model):
    bar = models.ForeignKey(BarModel)
    ...    

...and I'm doing FooModel.objects.all().select_related('bar') how can I defer() the field blah.

Thanks.

Chris W.
  • 37,583
  • 36
  • 99
  • 136

1 Answers1

13

Using Django's double-underscore notation as shown here.

FooModel.objects.all().select_related('bar').defer('bar__blah', ...)
solartic
  • 4,249
  • 3
  • 25
  • 26
  • 3
    Note: currently there is an opened issue with select_related for o2o relationship and 'only()' method ('defer' works fine). https://code.djangoproject.com/ticket/23051 (maybe would be useful for somebody) – Gleb Dec 25 '15 at 10:51