I am using django_filter, displaying it in my own way in template. I want to edit label of one of the filters. To do that I need to provide filter type. It works great in default but I can't figure out which filter type is used. This field in my model looks like this:
class Zamowienie(models.Model):
zam_kontrahent_fk = models.ForeignKey(Kontrahent, on_delete=models.CASCADE)
class Meta:
ordering = ['knt_kod']
verbose_name_plural = 'Kontrahenci'
objects = models.Manager()
def __str__(self):
return str(self.knt_nazwa)
On template it looks like typical select box. I figured out its not django_filters.ChoiceFilter
but django_filters.ModelChoiceFilter
. I tried using its queryset
option but it only displays list of primary_keys (1 instead of 'name' field from the table foreign_key is)
class ZamowienieFilter(django_filters.FilterSet):
zam_status = django_filters.ModelChoiceFilter(label='Status', queryset=Zamowienie.objects.order_by('zam_kontrahent_fk').values_list('zam_kontrahent_fk', flat=True).distinct())
class Meta:
model = Zamowienie
fields = [
'zam_kontrahent_fk',
]
Is it correct way to handle that? I didn't find a way to log default filter behaviour, it would help a lot, simply copying this fragment. Any idea how to display values, not list of primary_keys?
class Zamowienie
doesn't have a def __str__
defined. __str__
is used as the default representation for the ModelChoiceFilter options.
You're also using .values_list('zam_kontrahent_fk', flat=True)
, which will probably eliminate django-filter's ability to call __str__
.