0

I want to overwrite the __str__ method in Django admin when using the autocomplete_fields = () but the returned values are using __str__.

I have a form something like

class MyAdminForm(forms.ModelForm):
        
    placement = forms.Select(
        choices = Organisation.objects.active(),
    )

    class Meta:
        model = Lead
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['placement'].label_from_instance = lambda obj: f'{str(obj)} {obj.post_code}'

This will provide back a Select with the organisation name and post code in the dropdown fields. But there are some 80k choices so I need to using autocomplete. Within within admin.py I have

class LeadAdmin(admin.ModelAdmin):
    form = LeadAdminForm
    autocomplete_fields = ('placement',)

As soon as I add the autocomplete_fields I lose my postcode and it reverts to just showing the __str__

Hoa can I used autocomplete_fields and overwrite the __str__ method?

HenryM
  • 5,557
  • 7
  • 49
  • 105

1 Answers1

0

This question is answered through Benbb96 comment above which I've copied here so I can close it

So maybe this answer can help you : stackoverflow.com/a/56865950/8439435 – Benbb96

HenryM
  • 5,557
  • 7
  • 49
  • 105