POST请求更新数据字段 - AngularJS,Django

时间:2015-12-28 00:25:27

标签: angularjs django http-post httprequest

我有两种模式:

class Lecture(models.Model):
    lecture_no = models.IntegerField(null=True)
    title = models.CharField(max_length=128, unique=True, null=True)
    youtubeLink = models.CharField(max_length=128, unique=True, null=True)
    course = models.ForeignKey(Course, null=True)
    keywords = models.TextField(max_length=300, null=True)
    #Could add Next Rerun Date & Time

    def __str__(self):
        return self.title

class Notes(models.Model):
    notes = models.TextField(null=True)
    lecture = models.ForeignKey(Lecture, null=True, related_name='lecture')

    def __str__(self):
        return self.notes

这些序列化器:

class NotesSerializer(serializers.ModelSerializer):

    lecture = LectureSerializer(read_only=True, many=True)

    class Meta:
        model = Notes
        fields = ('id', 'notes', 'lecture')

class KeywordSerializer(serializers.ModelSerializer):

    lecture = LectureSerializer(read_only=True, many=True)

    class Meta:
        model = Keyword
        fields = ('id', 'notes', 'lecture')

和这些观点:

class LectureViewSet(viewsets.ModelViewSet):
    serializer_class = LectureSerializer

    def get_queryset(self):
        course_id = self.request.query_params.get('course',False)
        if course_id:
            lectures = Lecture.objects.filter(course=course_id)
        else:
            lectures = Lecture.objects.all()
        return lectures


class NotesViewSet(viewsets.ModelViewSet):
    queryset = Notes.objects.all()
    serializer_class = NotesSerializer

我正努力让它能够更新特定讲座的“笔记”字段。目前我正在使用POST http请求:

saveNotes: function(notes, lecture_id, callback) {
      $http({
        method: 'POST',
        url: apiRoute + 'notes/',
        data: {
          "notes": notes,
          "lecture_id": lecture_id
        }
      }).success(callback);
    }

但这只是每次都会向数据库添加一个新行。你如何更新字段?

由于

0 个答案:

没有答案