我正在关注 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"}