简体   繁体   English

.only().prefetch_related() 给出 'ManyToOneRel' 对象没有属性 'attname'

[英].only().prefetch_related() gives 'ManyToOneRel' object has no attribute 'attname'

i am fetching required fields of a table after a select_related() and prefetch_related() on the same query using .only()我正在使用 .only() 在同一查询上的 select_related() 和 prefetch_related() 之后获取表的必需字段

I can't seem to get the result from prefetch_related probably because .only() is restricting access to its result ?我似乎无法从 prefetch_related 获得结果,可能是因为 .only() 限制了对其结果的访问?

I have tried adding prefetch_related field in .only() as well but to no avail.我也尝试在 .only() 中添加 prefetch_related 字段,但无济于事。

Feedback.objects.all().select_related(
            'feedback_option'
        ).only(
            'feedback_option',

            # tried with and without either one or both of the following
            'feedback__remarks',
            'feedback__remarks__text'
        ).prefetch_related(
            Prefetch(
                'feedback__remarks',
                queryset=FeedbackRemark.objects.all().only('text'),
            ),
        )

I am getting the following exception because of this.因此,我收到以下异常。

AttributeError: 'ManyToOneRel' object has no attribute 'attname' AttributeError: 'ManyToOneRel' 对象没有属性 'attname'

I am using django 1.10.5我正在使用 Django 1.10.5

Have the same problem, solved it by replacing .only() with .defer(), listing all unused fields.有同样的问题,通过将 .only() 替换为 .defer() 来解决它,列出所有未使用的字段。 This shorten the memory used, at no additional db queries.这缩短了使用的内存,无需额外的数据库查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM