Couldn't get ToMany to work in Tastypie
I have been following the docs of Tastypie and found that I was completely 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 works based on 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 milestone attributes," traceback ":" Traceback (last call last): \ n \ n File \ C: \ Python27 \ lib \ site- packages \ tastypie \ resources. py \ "line 192, wrapped \ 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 dispatcher \ 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 dehydration \ n the_m2ms = getattr (bundle.obj, self.attribute) \ n \ nAttributeError: 'Project' object has no milestone attributes \ n "}
source to share
From the code posted I figure out that your Milestone model should look like this:
class Milestone(models.Model):
project = ForeignKey(Project, related_name='milestones')
UPDATED:
Your work 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'
source to share
:: Additional Resources ::
This blog has a great example of django-tastypie feedback
When I got AttributeError: "Parameters" object has no attribute "api_name" \ n "}
I had an example line printed as
milestones = fields.ToManyField('ProjectTrackerServer.projects.api.Milestone', 'projects', full=True)
Make sure you include "..api.Milestone Resource
~ Also ~
If you get an empty list where the relationship is, make sure the second argument you pass to ".ToManyField" matches your native name as specified in your model file.
milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', full=True)
class Milestone(models.Model):
project = models.ForeignKey(Project, related_name='projects')
...
source to share