Django Tastypie를 사용하는 REST API가 있습니다. 모델TastyPie를 사용하여 ToManyField 속성에서 요소를 정렬 할 수 있습니까?
class BlogPost(models.Model):
# class body omitted, it has a content and an author
class Comment(models.Model):
blog_post = models.ForeignKey(BlogPost, related_name="comments")
published = models.DateTimeField()
# rest of class omitted
나는 블로그 게시물을 요청하면
class CommentResource:
# omitted
class BlogPostResource(ModelResource):
comments = fields.ToManyField("resources.CommentResource",
attribute="comments")
내가 같은 것을 얻을 자원 다음 코드
을 감안할 때 : 그러나
GET: api/blogpost/4/
{
'content' : "....",
'author' : "....",
'comments' : ['api/comment/4/', 'api/comment/5']
}
를, 코멘트가 없습니다 반드시 모든 필드에 의해 정렬됩니다. 나는 그들이 특정 키 (published
)에 의해 정렬되어 있는지 확인하고 싶습니다
이것을 달성 할 방법이 있습니까?
class BlogPostResource(ModelResource):
comments = fields.ToManyField("resources.CommentResource",
attribute=lambda bundle: bundle.obj.comments.all().order_by("published"))
이렇게하면 댓글이 매김되지 않습니다. – rolnn