Django DRF + EmberJS2.6相关模型数据

时间:2016-08-09 01:07:52

标签: django ember.js django-rest-framework ember-data single-page-application

我有django应用程序,通过以下JSON响应处理restAPIs,我也有django-ember-adapter的ember应用程序。问题是我无法在API的子路由(http://localhost:8000/api/projects/4/tasks)中获取相关项。我可以通过http://localhost:8000/api/projects/4获得项目。我是否必须在API的项目的django模型中包含任务?我尝试在序列化程序中使用links,但似乎它不起作用。

如何成功完成项目任务

http://localhost:8000/api/projects/4上的json回复我可以通过this.store.findRecord('project', params.project_id)得到这个:

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

http://localhost:8000/api/projects/4/tasks上的json回复:

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

django模特:

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)

串行器:

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的项目模型

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

ember app的任务模型

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

更新 我试过嵌入式唱片mixin但没有运气

串行器中的

class ProjectSerializer(serializers.ModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta:
        model = Project
        fields = ('id', 'title', 'description', 'owner', 'tasks')

    def create(self, validated_data):
        """
        Create project using given validated data
        """
        return Project.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update project title description
        """
        instance.title = validated_data.get('title', instance.title)
        instance.description = validated_data.get(
                'description', instance.description)
        instance.save()


class TaskSerializer(serializers.ModelSerializer):
    project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())
    class Meta:
        model = Task
        fields = ('id', 'title', 'description', 'due_date', 'assign', 'project')

    def create(self, validated_data):
        return Task.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.description = validated_data.get('description', instance.description)
        instance.due_date = validated_data.get('due_date', instance.due_date)
        instance.assign = validated_data.get('assign', instance.assign)

我以这种方式尝试了链接:

import DRFSerializer from './drf';

export default DRFSerializer.extend({
  normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
    console.log(payload)
    payload.links: {
        tasks: tasks
      };
    });

    return this._super(...arguments);
  }
});

0 个答案:

没有答案