lundi 8 août 2016

Django DRF+ EmberJS2.6 related model data

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)


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')
});




Aucun commentaire:

Enregistrer un commentaire