In Django, Can I `defer()` fields in an object tha

2019-03-29 00:50发布

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.

1条回答
等我变得足够好
2楼-- · 2019-03-29 01:32

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

FooModel.objects.all().select_related('bar').defer('bar__blah', ...)
查看更多
登录 后发表回答