In Django, can I `defer ()` the fields in an object to request `select_related ()`

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

for example, if I have ...

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

... and I do FooModel.objects.all().select_related('bar') as I can defer() the blah field.

Thanks.

+6
source share
1 answer

Use the double-sided Django notation as shown here .

 FooModel.objects.all().select_related('bar').defer('bar__blah', ...) 
+6
source

Source: https://habr.com/ru/post/887501/


All Articles