Home > Enterprise >  Reverse Foriegn Key in Django Serializers
Reverse Foriegn Key in Django Serializers

Time:11-28

Suppose

class Post(models.Model):
    content = ...
    image = ...
    timestamp = ...

class PostLike(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,...)
    post = models.ForiegnKey(Post,...)
    timestamp = ...

and

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ("content","image","timestamp","liked")

Here liked could be true or false. It will be true if user (request.user) is in PostLike.

How do i create that liked field?

view

@login_required()
def AllPosts(request):
    obj = Posts.objects.all()
    serializer = PostSerializer(obj, many=True)
    result = JsonResponse(serializer.data, safe=False)
    return result

CodePudding user response:

Your serializer already has a field for the liked attribute, the only thing missing is a field:

class PostSerializer(serializers.ModelSerializer):
    liked = serializers.BooleanField(read_only=True)
    class Meta:
        model = Post
        fields = ('content', 'image', 'timestamp', 'liked')

What you need is a QuerySet where the posts have an extra attribute .liked You can annotate the Post objects with an extra attribute that specifies if the object was liked:

from django.db.models import Exists, OuterRef

def @login_required
def AllPosts(request):
    obj = Posts.objects.get_queryset(*args, **kwargs).annotate(
        liked=Exists(
            PostLike.objects.filter(user=request.user, post_id=OuterRef('pk'))
        )
    )
    serializer = PostSerializer(obj, many=True)
    result = JsonResponse(serializer.data, safe=False)
    return result
  • Related