Returns a QuerySet that will automatically
retrieve, in a single batch, related objects for each of the specified
lookups.
This has a similar purpose to select_related, in that both are
designed to stop the deluge of database queries that is caused by
accessing related objects, but the strategy is quite different.
The dehydrate method takes a now fully-populated bundle.data & make
any last alterations to it. This is useful for when a piece of data
might depend on more than one field, if you want to shove in extra
data that isn’t worth having its own field or if you want to
dynamically remove things from the data to be returned.
您的代码为每个类别执行额外的计数查询。这是一个很好的问题。在
Django将在
GROUP BY
语句中包含queryset的所有字段。注意.values()
和空的.group_by()
将字段设置为必需字段。在上面的
dict
对象是一个map[category\>;product\u count]。在可用于
^{pr2}$dehydrate
方法:如果这没用,试着在分类记录上保留类似的计数器,并使用信号来保持它的最新状态。在
注意类别通常是一个树状的存在,你可能还需要计算所有的子类别。在
在这种情况下,请查看包django-mptt。在
您可以使用
QuerSet
的prefetch_related方法来反向选择与u相关的。在Asper文档
如果您将脱水函数改为following,则数据库将被一次性命中。在
更新1
不应在dehydrate函数内初始化queryset。queryset应始终仅在
^{pr2}$Meta
类中设置。请看一下django-tastypie
文档中的以下示例。在根据
django-tastypie
documentation对dehydrate()
函数的正式django-tastypie
documentationdehydrate()
只用于对bundle.data包. 在相关问题 更多 >
编程相关推荐