问题
I'm following the Tastypie docs, and have found myself utterly stuck. I have the following:
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'
[UPDATE: The above API worked - based one the models below ]
Here are my models.
MODEL - Milestone:
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 - Project:
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)
I still get the same error:
{"error_message": "'Project' object has no attribute 'milestones'", "traceback": "Traceback (most recent call last):\n\n File \"C:\Python27\lib\site-packages\tastypie\resources.py\", line 192, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"C:\Python27\lib\site-packages\tastypie\resources.py\", line 406, in dispatch_detail\n return self.dispatch('detail', request, **kwargs)\n\n File \"C:\Python27\lib\site-packages\tastypie\resources.py\", line 427, in dispatch\n response = method(request, **kwargs)\n\n File \"C:\Python27\lib\site-packages\tastypie\resources.py\", line 1058, in get_detail\n bundle = self.full_dehydrate(bundle)\n\n File \"C:\Python27\lib\site-packages\tastypie\resources.py\", line 654, in full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle)\n\n File \"C:\Python27\lib\site-packages\tastypie\fields.py\", line 690, in dehydrate\n the_m2ms = getattr(bundle.obj, self.attribute)\n\nAttributeError: 'Project' object has no attribute 'milestones'\n"}
回答1:
From the posted code I figure out that your Milestone model should look like this:
class Milestone(models.Model):
project = ForeignKey(Project, related_name='milestones')
UPDATED:
Your working resource should look like this:
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:
::Additional resources::
This blog has an excellent django-tastypie reverse relationship example
When I had the error AttributeError: 'Options' object has no attribute 'api_name'\n"}
I had the example line typed out as
milestones = fields.ToManyField('ProjectTrackerServer.projects.api.Milestone', 'projects', full=True)
Make sure you are listing the "..api.MilestoneResource
~Also~
If you're getting an empty list where the relationships are, make sure that the second argument you pass into the ".ToManyField" matches your related name specified in your models file.
milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', full=True)
class Milestone(models.Model):
project = models.ForeignKey(Project, related_name='projects')
...
来源:https://stackoverflow.com/questions/14822280/unable-to-get-tomany-to-work-in-tastypie