How to get Django DRF + Ember 2.6 related model data from JSON response


#1

I have django app that handle restAPIs with following JSON response, also i have ember app with django-ember-adapter. The problem is i can not get related items in the sub route(http://localhost:8000/api/projects/4/tasks) of API. I can get projects by http://localhost:8000/api/projects/4. Am i have to include task in project’s django model in API?, I have tried using links in serializer but seems it’s not working.

How do i get project’s tasks successfully?

json response on http://localhost:8000/api/projects/4 i can get this by this.store.findRecord('project', params.project_id):

{
    "id": 4,
    "title": "test1 project",
    "description": "some project",
    "owner": "test1"
}

json response on http://localhost:8000/api/projects/4/tasks:

{
	"count": 2,
	"next": null,
	"previous": null,
	"results": [
		{
			"id": 5,
			"title": "4th",
			"description": "4tg description"
		},
		{
			"id": 6,
			"title": "",
			"description": ""
		}
	]
}

django models:

class Project(models.Model):
    """Canara project """
    title = models.CharField(max_length=50, blank=True, default='')
    created = models.DateTimeField(auto_now_add=True)
    description = models.CharField(max_length=255, blank=True)
    active = models.BooleanField(default=True)
    owner = models.ForeignKey(User, related_name='projects', null=True)

    def __unicode__(self):
        return self.title


class Task(models.Model):
    title = models.CharField(max_length=55, blank=True)
    description = models.CharField(max_length=255, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    due_date = models.DateField(blank=True, null=True)
    assign = models.ForeignKey(User, null=True)
    project = models.ForeignKey(Project, null=True)

serializer:

class ProjectViewSet(ModelViewSet):
	"""
	API endpoint for project
	"""
	authentication_classes = [TokenAuthentication, SessionAuthentication]
	permission_classes = (IsAuthenticated, IsOwner)
	serializer_class = ProjectSerializer

	def get_queryset(self):
		"""
		Get user's project
		"""
		return Project.objects.filter(members__member=self.request.user)
		
	def perform_create(self, serializer):
		"""
		Set user as an owner when create project
		"""
		serializer.save(owner=self.request.user)

class TaskViewSet(ModelViewSet):
	"""
	API for task
	"""
	authentication_classes = [TokenAuthentication, SessionAuthentication]
	permission_classes = [IsAuthenticated,]
	serializer_class = TaskSerializer
	queryset = Task.objects.all()

ember app’s project model

export default Model.extend({
  title: attr(),
  description: attr(),
});

ember app’s task model

export default Model.extend({
  title: attr(),
  description: attr(),
  due_date: attr('date'),
  assign: belongsTo('user'),
  project: belongsTo('project')
});