简体   繁体   中英

Django annotate field value from another model

I want to annotate MyModel queryset with a value from another History model. My models relations are the following:

class Stage(models.Model):
    name = models.CharField()


class History(models.Model):
    mymodel = models.ForeignKey(
        MyModel,
        on_delete=models.CASCADE,
    )
    stage = models.ForeignKey(
        Stage,
        on_delete=models.DO_NOTHING
     )
    created_at = models.DateTimeField(
        auto_now_add=True
    )

class MyModel(models.Model):
    id = models.AutoField(
        primary_key=True,
    )
    ...

Approximate operation I'd like to perform in the ViewSet (order_by I need to get the latest stage):

class MyModelViewSet(viewsets.ModelViewSet):
    ...
    def get_queryset(self):
        qs = super(MyModelViewSet, self).get_queryset()
        qs = qs.annotate(last_stage_name=Value(
            History.objects.filter(mymodel__id=F('id')).order_by('-created_at')[0].stage.name,
            output_field=CharField()))

But I get the same stage for all objects from the first object instead of the latest stage of every object, so the logics of operation is wrong. I suppose I should get rid of History.objects.filter but can't find appropriate solution.

You can work with a Subquery expression [Django-doc] :

from django.db.models import OuterRef, Subquery

class MyModelViewSet(viewsets.ModelViewSet):
    
    # …
    
    def get_queryset(self):
        return super().get_queryset().annotate(
            last_stage_name=
                History.objects.filter(
                    mymodel_id=
                ).order_by('-created_at').values('stage__name')[:1]
            
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM