How to filter Django ManyToManyField by Id

32 Views Asked by At

I have two models:

class Category(models.Model):
        name = models.CharField(max_length=255)

class Company(models.Model):
        name = models.CharField(max_length=255)
        category = models.ManyToManyField(Category, related_name='companies')
        products = models.TextField()
        bulletpoints = models.TextField()
        website = models.CharField(max_length=750)
        articles = models.TextField()

Now I want to filter the Objects in the Company model by the id of the categories, which I put them in.

I tried several options including everything from that question: https://stackoverflow.com/questions/2218327/django-manytomany-filter for example: Company.objects.filter(companies__in=[category_id]) Company.objects.filter(companies__id=category_id) Company.objects.filter(category_id=category_id) Company.objects.filter(companies__id__in=category_id)

0

There are 0 best solutions below