Example #1
0
 def get_comments(self, obj):
     comments = Comment.objects.filter(post=obj)
     if comments.exists():
         comments = CommentListSerializer(comments, many=True).data
     else:
         comments = None
     return comments
Example #2
0
 def get_comments(self, obj):
     comments_queryset = Comment.objects.filter_by_instance(obj)
     serializer_context = {'request': self.context['request']}
     comments = CommentListSerializer(comments_queryset,
                                      many=True,
                                      context=serializer_context).data
     return comments
Example #3
0
 def get_comments(self, obj):
     c_qs = Comment.objects.filter_by_instance(obj)
     comments = CommentListSerializer(c_qs,
                                      many=True,
                                      context={
                                          'request': request
                                      }).data
     return comments
Example #4
0
 def get_comments(self, obj):
     """this filter_by_instance and get_content_type was defined in our models
       note you have to serialize it that is why i use the
       CommentSerializer i created for the content_type and the object_id
      """
     # content_type = obj.get_content_type  # not using it
     # object_id = obj.id  # already defined a method for it
     c_qs = Comment.objects.filter_by_instance(obj)
     comments = CommentListSerializer(c_qs, many=True).data
     return comments
Example #5
0
    def get_comments(self, obj):
        cmts_qs = Comment.objects.filter_by_instance(obj)

        # Foi necessário passar "context={'request': None}"
        # http://www.django-rest-framework.org/api-guide/serializers/#absolute-and-relative-urls
        comments = CommentListSerializer(cmts_qs,
                                         many=True,
                                         context={
                                             'request': None
                                         }).data
        return comments
Example #6
0
	def get_comments(self, obj):
		#content_type = obj.get_content_type
		#ovject_id = obj.id
		c_qs = Comment.objects.filter_by_instance(obj)
		comments = CommentListSerializer(c_qs, many=True).data
		return comments
Example #7
0
 def get_comments(self, obj):
     c_qs = Comment.objects.filter_by_instance(obj)
     comments = CommentListSerializer(c_qs, many=True).data
     return comments
class MovieSerializer(serializers.ModelSerializer):
  # comments = serializers.RelatedField(many=True) ##gets comment id
  # comments = serializers.StringRelatedField(many= True) ##gives (Comment Related Object) As response
  # comments = serializers.PrimaryKeyRelatedField(many= True,read_only=True) ##gets comment id
  # comments = serializers.SerializerMethodField(source = 'comment', many=True) ##gets comment id
  # comments = serializers.SerializerMethodField()
  comments = CommentListSerializer(many=True)

  class Meta:
    model=Movie
    # fields = ('backdrop_path','title','budget','tmdb_id','orginal_title','overview','poster_path','popularity','status','release_date','runtime','tagline','video','vote_count','vote_average','')
    fields = ('id','backdrop_path','title','budget','tmdb_id','orginal_title','overview','poster_path','popularity','status','release_date','runtime','tagline','video','vote_count','vote_average','comments')
    # depth=1










# class MovieCommentSerializer(serializers.ModelSerializer):
#   user = UserSerializer(queryset=User.objects.all(),required=False, allow_null=True, default=None)
#   # user_id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(),required=False, allow_null=True, default=None)
#   comment = serializers.CharField(
#     max_length=255,
#     help_text=(
#       'Required. 3-32 characters.'
#     ),
#   )
#   comment = serializers.CharField(
#     max_length=255,
#     help_text=(
#       'Required. 3-32 characters.'
#     ),
#   )
#   movie_id = serializers.CharField(
#     max_length=255,
#     help_text=(
#       'Required. 3-32 characters.'
#     ),
#   )
#   # movie_id = serializers.PrimaryKeyRelatedField(queryset=Movie.objects.all(),required=False, allow_null=True, default=None )
#   class Meta:
#     model=Comment
#     fields = ('id','comment','movie_id','created','user_id')  
#     # extra_kwargs = {'user_id': {'default': serializers.CurrentUserDefault()}}
  



  # def create(self,validated_data):
  #   user_id = validated_data.pop('user_id')
  #   # user = validated_data.pop('user')
  #   movie_id = validated_data.pop('movie_id')
  #   comment = validated_data.pop('comment')

  #   comment_object = Comment.objects.create(comment = comment, movie_id = movie_id,user_id =user_id,)
  #   # comment_object.user = validated_data.pop('user')
  #   # user = self.request.user
  #   # print(user)

  #   return comment_object
Example #9
0
 def get_comment_count(self,obj):
     c_qs = Comment.objects.filter_by_instance(obj)
     comments = CommentListSerializer(c_qs,many=True).data
     comment_count = len(comments)
     print('the comment count is ',comment_count)
     return comment_count
Example #10
0
 def get(self, request, format=None):
     comment = Comment.objects.all()
     serializer = CommentListSerializer(comment, many=True)
     print(serializer)
     return Response(serializer.data)