4

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

接口:

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

型号 - 项目:

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' object has no attribute 'milestones'", "traceback": "Traceback (最近一次调用最后一次):\n\n File \"C:\Python27\lib\site-packages\tastypie \resources.py\",第 192 行,在包装器中\n response = callback(request, *args, **kwargs)\n\n File \"C:\Python27\lib\site-packages\tastypie\resources.py \",第 406 行,在 dispatch_detail\n return self.dispatch('detail', request, **kwargs)\n\n File \"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 文件 \"C:\Python27\lib\site-packages\tastypie\resources.py\",第 654 行,在 full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle)\n\n 文件 \"C:\Python27\lib\site-packages\tastypie\fields.py\",第 690 行,在 dehydrate\n the_m2ms = getattr(bundle.obj, self.attribute)\n\nAttributeError: 'Project' object has no attribute 'milestones'\n"}

4

2 回答 2

4

::其他资源::

这个博客有一个优秀的 django-tastypie 反向关系示例

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

我将示例行输入为

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

确保您列出“..api.Milestone资源

~还有~

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

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

class Milestone(models.Model):
    project = models.ForeignKey(Project, related_name='projects')
    ...
于 2016-01-25T20:05:05.997 回答
3

从发布的代码中,我发现您的 Milestone 模型应如下所示:

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'
于 2013-02-12T10:43:07.037 回答