Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, Can I `defer()` fields in an object that's being queried by `select_related()`

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.

like image 916
Chris W. Avatar asked May 07 '11 04:05

Chris W.


1 Answers

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

FooModel.objects.all().select_related('bar').defer('bar__blah', ...)
like image 183
solartic Avatar answered Oct 05 '22 22:10

solartic