无法让ToMany在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行,在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 File \“C:\ Python27 \ lib \ site-packages \ tastypie \ fields.py \”,第690行,脱水\ n the_m2ms = getattr(bundle.obj,self.attribute) \ n \ n属性错误:'Project'对象没有属性'mi lestones' \ n“}

回答(2)

2 years ago

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

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'

2 years ago

::其他资源::

This blog has an excellent django-tastypie reverse relationship example

当我有错误 AttributeError: 'Options' object has no attribute 'api_name'\n"}

我将示例行输入为

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

确保列出“..api.Milestone Resource

〜还〜

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

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

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