[Fixed]-Django query that excludes results if id from one table is in another

1👍

With access to the request object in the view, you can get a list of item_ids for the current user from the Seen table and filter using Django’s in operator.

unseen_items = Item.objects.exclude(
    pk__in=Seen.objects.filter(user_id=request.user).values_list(
        'item_id', flat=True
    )
)

See here for info on in and here for info on values_list.

Leave a comment