How to modify the queryset of a ManyToMany field in Django Framework


my question is to know if you can and how it would be to modify the QuerySet by default that a field with the ManyToMany model brings, because as I understand the default query is:


But I would like to change that query by a filter, I know how to assemble the filter query but I do not know where or where to place or modify the query, that is, I do not know if it is in the ModelAdmin or in the Models.

asked by Diego Asencio 29.11.2018 в 23:21

1 answer


It would be something like Modelo.objects.filter().values('valor1', 'valor2') or Modelo.objects.filter().values_list('valor1', 'valor2')



Returns a QuerySet that returns dictionaries, instead of model instances, when used as iterable. Each of these dictionaries represents an object, with the keys corresponding to the attribute names of the model objects.


The values_list (): This is similar, values () except that instead of returning dictionaries, it returns tuples when iterated. Each tuple contains the value of the corresponding field or expression that is passed to the called values_list (), so the first element is the first field, etc.

answered by 30.11.2018 в 17:19