【发布时间】:2020-12-09 05:28:18
【问题描述】:
我创建了一个方法字段,例如
matchCount= serializers.SerializerMethodField('get_match')
def get_match(self, obj):
likes = obj.post.count
#here will be logic of almost 120 lines so cant use annotate*
return likes / time if time > 0 else likes
现在我想根据这个提交的 matchCount 进行订购。在这种情况下,最好的解决方案是什么?直到现在我发现其他开发人员正在使用注释等,但由于这个逻辑是动态的,并且会在运行时计算匹配,我该怎么做才能根据这个字段进行排序?
注意:如果你们对此善意建议有更好的解决方案,我们将不胜感激
【问题讨论】:
标签: django django-rest-framework django-orm