N
N
NyxDeveloper2021-01-22 16:26:56
Django
NyxDeveloper, 2021-01-22 16:26:56

How to filter queryset by multi select?

I add the ability to select multiple parameters to the search form in the application, I decided to use select multiple, but I don’t know how to filter with multiple values. Please suggest some concise solution. Thank you!
Is it even possible to bind some cool multiselect widget to django and use QuerySet.filter() to filter the list of objects?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Ivanov, 2021-01-22
@CraMas

I did a little crooked, but I did not find another way

class TechniqueGroup(models.Model):
    name = models.CharField(max_length=250, verbose_name='Тематика', blank=True, null=True, unique=True)
    technique = models.ManyToManyField(Technique, verbose_name='техника', related_name='group', blank=True, )

        def grouplist(self):
        group = []
        for tech in self.technique.all():
            group.append(tech.id)
        return group

    def __str__(self):
        return self.name

and then it was already possible to do
QuerySet.filter (fieldname__id__in = techniquegroup.grouplist()) in the filter
if there is a more concise code, then I will also be glad.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question