-2

Here is my models

class Mediums(models.Model):
    medium_name = models.CharField(max_length=255)


class Artwork(models.Model):
    title             = models.CharField(max_length=255, blank=True, null=True)
    mediums           = models.ManyToManyField(Mediums, blank=True, related_name="artwork")

    class Meta:
        db_table = 'artwork'

I am using django-reft-framework .

How can i fetch artwork by filtering multiple medium_id. I checked drf doc could not find option to filter ManyToManyField

Pse take a look

  • You can use `__in` with `mediums` so: `Artwork.objects.filter(mediums__in=Medium.objects.filter())`. You can find the doc and some examples [here](https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/#many-to-many-relationships) – Brian Destura Aug 19 '21 at 07:23
  • How can i add that in drf filterset_fields – Kiran S youtube channel Aug 19 '21 at 07:25
  • if you want filtering at a url level, you can look into https://www.django-rest-framework.org/api-guide/filtering/ or https://django-filter.readthedocs.io/en/stable/ and please make it clear in the question – António Caeiro Aug 19 '21 at 08:38

1 Answers1

0

documentation: https://docs.djangoproject.com/en/3.2/ref/models/querysets/#in

Artwork.objects.filter(mediums__in=medium_ids_or_objects)

similar question Django ManyToMany filter()

António Caeiro
  • 135
  • 1
  • 9