David Young
David Young

Reputation: 129

Django filtering: from a list of IDs

I'm using Django Rest Framework.

The model class is

class MyModel(models.Model):
       id = models.CharField(max_length=200)
       name = models.CharField(max_length=200)
       genre = models.CharField(max_length=200)

And what I have set up so far is that, when the user does a POST request, the backend will take the request data and run a python script (which takes some parameters from the request data) which will in turn return a list of IDs corresponding to the "id" in MyModel. But the problem is, let's say I want to return only the ids that point to the model instances with genre "forensic", how do I do that?

I don't really have a clue how to do that, apart from doing a query on each id returned by the python script and filtering out the ones I want based on the genre returned from the query?

Upvotes: 1

Views: 2401

Answers (1)

ruddra
ruddra

Reputation: 52028

Maybe you can try like this:

MyModel.objects.filter(id__in=IDS, genre='forensic').values_list('id', flat=True)  # assuming IDS come from the script

Upvotes: 3

Related Questions