【发布时间】:2020-01-21 14:47:41
【问题描述】:
考虑一个具有多个属性的对象。
class Object(models.Model):
name= Charfield(max_length=80)
class Property(models.Model):
object= ForeignKey(Object, related_name='properties')
name= Charfield(max_length=80)
value= Charfield(max_length=80)
我想为对象模型提供一个 api 可访问的列表视图,但我无法按属性值对 ObjectList 进行排序,也无法按属性值对 ObjectList 进行排序。
为了使示例更具体,您可以想象 Object 具有 book 类型,并且 book 对象除了其他属性外,还具有名称为 title 的属性和诸如 20,000 Leagues Under the Sea 之类的值。我希望能够指定?order_by=title 并能够过滤?title__contains=League(或至少?title="20,000 Leagues Under the Sea"。
我已经尝试使用 title="20,000 Leagues Under the Sea" 注释 Object,并且我能够在 ObjectSerializer 中显示标题,就好像它是一个字段一样,但是按此字段排序不起作用。
【问题讨论】:
-
您能在此处提供您的视图集示例吗?
标签: django django-models django-rest-framework django-views