【发布时间】:2017-08-25 08:44:42
【问题描述】:
我想为每个 parent_types(“top”、“bottom”、“shoes”)检索 5 件衣服
user.clothes_set.filter(type="top")[:5]
user.clothes_set.filter(type="bottom")[:5]
user.clothes_set.filter(type="shoes")[:5]
我想以更有效的方式做到这一点。 (三个过滤器很讨厌!)
top, bottom, shoes = user.clothes_set.filter(~~~) <- retrieve `5 items each`
这里是预期的布模型
class Clothes(models.Model):
id
type = # top, bottom and shoes
owner = ForeignField # some one who post
我应该重新设计模型吗?我应该将“类型”字段排除在类中吗?还是不可能?
【问题讨论】:
标签: django django-models django-views django-queryset