The field I need to search in belongs to a reverse 'diariaviagem' to 'viagem' relationship
class DiariaViagem(models.Model):
viagem = models.ForeignKey(Viagem, on_delete=models.CASCADE)
fonte_pagamento = models.ForeignKey(FontePagamento, null=True, blank=True, on_delete=models.CASCADE)
class ViagemAdmin(admin.ModelAdmin):
list_filter = (FonteFilter,)
filter
class FontePagamentoViagemChangeListAutocomplete(autocomplete.Select2QuerySetView):
model = FontePagamento
def get_queryset(self):
qs = self.model.objects.all()
if self.q:
qs = qs.filter(Q(diariaviagem__fonte_pagamento__codigo__icontains=self.q) | Q(
diariaviagem__fonte_pagamento__descricao__icontains=self.q))
return qs
class FonteFilter(FilterSelect2):
title = 'Fonte de pagamento' # filter's title
field_name = 'diariaviagem_set__fonte_pagamento' # field name - ForeignKey to Country model
autocomplete_url = 'fonte_pagamento_viagem_change_list_autocomplete' # url name of Country autocomplete view
is_placeholder_title = False
widget_attrs = {'data-placeholder': 'Selecione'}
The code displays the following error:
type object 'Viagem' has no attribute 'diariaviagem_set__fonte_pagamento'
If I just pass 'diariaviagem_set' or a property it returns:
'ReverseManyToOneDescriptor' object has no attribute 'get_queryset'
Can I do something like this?