def get_comments(request): event = Event.objects.get(pk=request.data['event']) comments = event.comment_set.filter(c_self=None) l = [] for c in comments: l.append(CommentsSerializer(c.comment_set.all(), many=True).data) serializer = CommentsSerializer(comments, many=True) return Response({'comments': serializer.data, 'replies': list(l)})
def put(self, request, *args, **kwargs): comment = Comment.objects.get(id=request.data.get('commentID')) meeting = Meeting.objects.get(id=request.data.get('meetingID')) user = User.objects.get(id=request.data.get('userID')) comment.description = request.data.get('description') comment.meeting = meeting comment.user = user comment.save() serializer = CommentsSerializer(instance=comment, data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.data)
def post(self, request, *args, **kwargs): comment = Comment() questtionaire = Questionnaire.objects.get( id=request.data.get('questtionaire')) user = User.objects.get(id=request.data.get('user')) comment.description = request.data.get('description') comment.questtionaire = questtionaire comment.user = user comment.save() serializer = CommentsSerializer(instance=comment, data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.data)
class TouristSpotsSerializer(ModelSerializer): attractions = AttractionsSerializer(many=True) comment = CommentsSerializer(many=True) address = AddressSerializer() fullDescription = SerializerMethodField() class Meta: model = TouristSpots fields = ('id', 'name', 'description', 'approved', 'image', 'address', 'attractions', 'comment', 'fullDescription') def get_fullDescription(self, obj): return '%s - %s' % (obj.name, obj.description)
class TouristSpotSerializer(ModelSerializer): attractions = AttractionsSerializer(many=True) comments = CommentsSerializer(many=True) assessments = AssessmentSerializer(many=True) # end = AddressSerializer() description_complete = SerializerMethodField() class Meta: model = TouristSpot fields = ( 'id', 'name', 'descriptions', 'approved', 'photo', 'attractions', 'comments', 'assessments', 'description_complete', 'description_complete_two', ) def get_description_complete(self, obj): return '%s - %s' % (obj.name, obj.descriptions)
def get_comments(self, obj): content_type = obj.get_content_type object_id = obj.id c_qs = Comments.objects.filter_by_instance(obj) comments = CommentsSerializer(c_qs, many=True).data return comments