【问题标题】:In Django, Can I `defer()` fields in an object that's being queried by `select_related()`在 Django 中,我可以在一个由`select_related()` 查询的对象中`defer()` 字段吗
【发布时间】:2011-08-20 14:44:26
【问题描述】:

在我的 Django 应用程序中,我想在 QuerySet 上使用 select_related() 来“关注”ForeignKey 字段,但我只需要访问“关注”模型实例上的一些字段。我可以在我的“关注”字段中以某种方式使用defer() 方法吗?

例如,如果我有...

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

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

...我在做FooModel.objects.all().select_related('bar') 我怎么能defer() 字段blah

谢谢。

【问题讨论】:

    标签: python django django-queryset django-select-related


    【解决方案1】:

    使用 Django 的双下划线符号,如图所示 here

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

    【讨论】:

    • 注意:目前存在与 o2o 关系的 select_related 和 'only()' 方法('defer' 工作正常)的未解决问题。 code.djangoproject.com/ticket/23051(也许对某人有用)
    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2011-12-30
    • 2022-12-02
    相关资源
    最近更新 更多