![](/img/trans.png)
[英]AttributeError: 'Resume' object has no attribute 'prefetch_related'
[英].only().prefetch_related() gives 'ManyToOneRel' object has no attribute 'attname'
我正在使用 .only() 在同一查詢上的 select_related() 和 prefetch_related() 之后獲取表的必需字段
我似乎無法從 prefetch_related 獲得結果,可能是因為 .only() 限制了對其結果的訪問?
我也嘗試在 .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'),
),
)
因此,我收到以下異常。
AttributeError: 'ManyToOneRel' 對象沒有屬性 'attname'
我正在使用 Django 1.10.5
有同樣的問題,通過將 .only() 替換為 .defer() 來解決它,列出所有未使用的字段。 這縮短了使用的內存,無需額外的數據庫查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.