无法让ToMany在Tastypie工作

时间:2013-02-11 22:40:10

标签: python django tastypie

我正在关注Tastypie文档,发现自己完全陷入困境。我有以下内容:

API:

class ProjectResource(ModelResource):
    milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', related_name='project', full=True)

    class Meta:
        queryset = Project.objects.all()
        resource_name = 'project'


class MilestoneResource(ModelResource):
    project = fields.ToOneField('ProjectTrackerServer.projects.api.ProjectResource', 'project')

    class Meta:
        queryset = Milestone.objects.all()
        resource_name = 'milestone'

[更新:以上API工作 - 基于以下模型]

以下是我的模特。

模特 - 里程碑:

from django.db import models
from ProjectTrackerServer.projects.models import Project
class Milestone(models.Model):
    project = models.ForeignKey(Project, related_name='projects')
    name = models.TextField()
    start_date = models.DateField()
    due_date = models.DateField()
    completed_date = models.DateField()
    description = models.TextField()
    status = models.IntegerField()

def __unicode__(self):
    return self.name

MODEL - 项目:

from django.db import models
from django.template.defaultfilters import slugify

class Project(models.Model):
     name = models.CharField(max_length=200)
     start_date = models.DateField()
     end_date = models.DateField()
     pm_id = models.IntegerField()
     status = models.IntegerField()
     slug = models.SlugField()

     def __unicode__(self):
         return self.name

     def save(self, *args, **kwargs):
         if not self.slug:
             self.slug = slugify(self.name)[:50]
             return super(Project, self).save(*args, **kwargs)

我仍然得到同样的错误:

{“error_message”:“'Project'对象没有属性'里程碑'”,“traceback”:“Traceback(最近一次调用最后一次):\ n \ n文件\”C:\ Python27 \ lib \ site- packages \ tastypie \ resources.py \“,第192行,在包装器中\ n response = callback(request,* args,** kwargs)\ n \ n文件\”C:\ Python27 \ lib \ site-packages \ tastypie \ resources.py \“,第406行,在dispatch_detail \ n中返回self.dispatch('detail',request,** kwargs)\ n \ n文件\”C:\ Python27 \ lib \ site-packages \ tastypie \ resources。 py \“,第427行,在dispatch \ n response = method(request,** kwargs)\ n \ n File \”C:\ Python27 \ lib \ site-packages \ tastypie \ resources.py \“,第1058行, in get_detail \ n bundle = self.full_dehydrate(bundle)\ n \ n File \“C:\ Python27 \ lib \ site-packages \ tastypie \ resources.py \”,第654行,在full_dehydrate中\ nsundal.data [field_name ] = field_object.dehydrate(bundle)\ n \ n文件\“C:\ Python27 \ lib \ site-packages \ tastypie \ fields.py \”,第690行,脱水\ n the_m2ms = getattr(bundle.obj,self .attribute)\ n \ n属性错误:'项目'对象没有属性'里程碑'\ n“}

2 个答案:

答案 0 :(得分:4)

::其他资源::

This blog has an excellent django-tastypie reverse relationship example

当我遇到错误属性错误:'选项'对象没有属性' api_name' \ n"}

我将示例行输入为

milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', full=True)

class Milestone(models.Model):
    project = models.ForeignKey(Project, related_name='projects')
    ...

确保列出" .. api.Milestone 资源

〜还〜

如果您获得关系所在的空列表,请确保将第二个参数传递给" .ToManyField"匹配模型文件中指定的相关名称。

...
var resp = new HttpResponseMessage(HttpStatusCode.OK) {
    Content = new StreamContent(new MemoryStream(byte_data))
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
    FileName = "MyFile.csv"
};

return resp;

答案 1 :(得分:3)

从发布的代码中我发现你的里程碑模型应如下所示:

class Milestone(models.Model):
    project = ForeignKey(Project, related_name='milestones')

更新:

您的工作资源应如下所示:

class ProjectResource(ModelResource):
  milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', full=True)
class Meta:
    queryset = Project.objects.all()
    resource_name = 'project'


class MilestoneResource(ModelResource):
    project = fields.ForeignKey(Project, 'project')

    class Meta:
        queryset = Milestone.objects.all()
        resource_name = 'milestone'