包括在Django Tastypie API子资源 [英] Including child resources in a Django Tastypie API
问题描述
我打算使用Django和Tastypie网站的REST API,并
我有一个艰难的时间搞清楚了正确的方式包括
子资源在返回的资源。
作为一个沙箱中,我做了一个小的应用程序了票务模型和TicketComment
模型,其中的意见属于一票。我看着Tastypie食谱
方对嵌套资源(http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources)
但我有一个很难理解为什么我应该这样做。下面的code
使用django.forms.models.model_to_dict()来获得各种意见,门票,
但我想,必须有一个疑难杂症在这里的某个地方。
有一个原因,我不应该做我现在正在做什么?此外,有没有一个
清洁感觉的图案为这个比什么在菜谱列出?
型号如下:
#张/ models.py从django.db进口车型客位机票(models.Model):
标题= models.CharField(MAX_LENGTH = 200)
create_ts = models.DateTimeField(auto_now_add = TRUE)
submitter_email = models.EmailField()
PRIORITY_CHOICES =(
(H,高),
('M','中'),
(L,低),)
优先级= models.CharField(MAX_LENGTH = 1,选择= PRIORITY_CHOICES)
说明= models.TextField()
STATUS_CHOICES =(
(新,最新及放大器;无人认领'),
('WIP','工作进行中),
('RES','解决'),
(CLS,关闭))
状态= models.CharField(MAX_LENGTH = 3,选择= STATUS_CHOICES) 高清__uni code __(个体经营):
返回<门票数:%d:%S>中%(self.id,self.title,)类TicketComment(models.Model):
票= models.ForeignKey(门票)
comment_ts = models.DateTimeField(auto_now_add = TRUE)
commenter_email = models.EmailField()
注释= models.TextField() 高清__uni code __(个体经营):
返回< TicketComment数:%d:%d取代; %(self.ticket.id,self.id,)
资源如下:
#张/ api.py从tastypie进口领域
从tastypie.resources进口ModelResource
从tickets.models进口票,TicketComment
从django.forms.models进口model_to_dict类TicketResource(ModelResource): 类元:
查询集= Ticket.objects.all()
RESOURCE_NAME ='票' 高清脱水(个体经营,束):
评论= TicketComment.objects.filter(门票= bundle.data ['身份证'])
bundle.data [意见] = [model_to_dict(三)对于C的评论]
回包类TicketCommentResource(ModelResource):
票= fields.ForeignKey(TicketResource,'票') 类元:
查询集= TicketComment.objects.all()
RESOURCE_NAME =评论
输出如下所示:
{
注释: [
{
评论:这是第一个评论。
commenter_email:me@example.com
ID:1,
门票:1
},
{
评论:这是第二个评论。
commenter_email:me@example.com
ID:2,
门票:1
}
]
create_ts:2011-10-17T15:55:11.372000,
介绍:这是第一票。
编号:1,
优先权:M
resource_uri:/ API / V1 /票/ 1 /
状态:NEW,
submitter_email:me@example.com
标题:第一票
}
您正在寻找相关领域:<一href=\"http://django-tastypie.readthedocs.org/en/latest/fields.html#relationship-fields\">http://django-tastypie.readthedocs.org/en/latest/fields.html#relationship-fields
I'm planning a site with Django and Tastypie for the REST API, and I'm having a tough time figuring out the "right" way to include child resources in a returned resource.
As a sandbox, I made a small app with a Ticket model and a TicketComment model, where comments belong to a ticket. I looked at the Tastypie Cookbook recipe on nested resources (http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources), but I'm having a hard time understanding why I should do that. The code below uses django.forms.models.model_to_dict() to get the comments into the ticket, but I'm thinking there must be a "gotcha" here somewhere.
Is there a reason I shouldn't do what I'm doing now? Also, is there a cleaner-feeling pattern for this than what's listed in the cookbook?
Models are as follows:
# 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,)
Resources are as follows:
# 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'
Output is as follows:
{
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"
}
You're looking for related fields: http://django-tastypie.readthedocs.org/en/latest/fields.html#relationship-fields
这篇关于包括在Django Tastypie API子资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!