Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
Reverse Foriegn Key in Django Serializers

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
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda