REST API 用の Django と Tastypie を使用したサイトを計画していますが、返されたリソースに子リソースを含める「正しい」方法を見つけるのに苦労しています。
サンドボックスとして、Ticket モデルと TicketComment モデルを使用して、コメントがチケットに属する小さなアプリを作成しました。ネストされたリソースに関する Tastypie Cookbook のレシピ (http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources) を見ましたが、なぜそうするべきなのか理解するのに苦労しています. 以下のコードは django.forms.models.model_to_dict() を使用してコメントをチケットに取り込みますが、どこかに「落とし穴」があるに違いないと思います。
私が今やっていることをやるべきではない理由はありますか?また、クックブックに記載されているものよりもクリーンな感じのパターンはありますか?
モデルは次のとおりです。
# tickets/models.py
from django.db import models
class Ticket(models.Model):
title = models.CharField(max_length=200)
create_ts = models.DateTimeField(auto_now_add=True)
submitter_email = models.EmailField()
PRIORITY_CHOICES = (
('H', 'High'),
('M', 'Medium'),
('L', 'Low'),)
priority = models.CharField(max_length=1, choices=PRIORITY_CHOICES)
description = models.TextField()
STATUS_CHOICES = (
('NEW', 'New & Unclaimed'),
('WIP', 'Work In Progress'),
('RES', 'Resolved'),
('CLS', 'Closed'),)
status = models.CharField(max_length=3, choices=STATUS_CHOICES)
def __unicode__(self):
return "<Ticket:%d:%s>" % (self.id, self.title,)
class TicketComment(models.Model):
ticket = models.ForeignKey(Ticket)
comment_ts = models.DateTimeField(auto_now_add=True)
commenter_email = models.EmailField()
comment = models.TextField()
def __unicode__(self):
return "<TicketComment:%d:%d>" % (self.ticket.id, self.id,)
リソースは次のとおりです。
# tickets/api.py
from tastypie import fields
from tastypie.resources import ModelResource
from tickets.models import Ticket, TicketComment
from django.forms.models import model_to_dict
class TicketResource(ModelResource):
class Meta:
queryset = Ticket.objects.all()
resource_name = 'ticket'
def dehydrate(self, bundle):
comments = TicketComment.objects.filter(ticket=bundle.data['id'])
bundle.data['comments'] = [model_to_dict(c) for c in comments]
return bundle
class TicketCommentResource(ModelResource):
ticket = fields.ForeignKey(TicketResource, 'ticket')
class Meta:
queryset = TicketComment.objects.all()
resource_name = 'comment'
出力は次のとおりです。
{
comments: [
{
comment: "This is the first comment.",
commenter_email: "me@example.com",
id: 1,
ticket: 1
},
{
comment: "This is the second comment.",
commenter_email: "me@example.com",
id: 2,
ticket: 1
}
],
create_ts: "2011-10-17T15:55:11.372000",
description: "This is the first ticket.",
id: "1",
priority: "M",
resource_uri: "/api/v1/ticket/1/",
status: "NEW",
submitter_email: "me@example.com",
title: "First Ticket"
}