0

I followed the accepted answer here to alter the order of items in a foreign key select list in my Django admin. The reordering works great, but the little add icon has disappeared from after the select list. How can I get it back?

class GameForm(forms.ModelForm):
    publisher = forms.ModelChoiceField(queryset=Publisher.objects.order_by('name'))
    platform = forms.ModelChoiceField(queryset=Platform.objects.order_by('platform'))

    class Meta:
        model = Game

class GameAdmin(admin.ModelAdmin):
    list_display = ('name','platform','publisher','age_rating','buy_date','pub_date')
    form = GameForm

admin.site.register(Game, GameAdmin)

1 Answer 1

2

Your form overrides admin application fields RelatedFieldWidgetWrapper widget

To prevent this queryset should be changed in __init__ method

class GameForm(forms.ModelForm):
    class Meta:
        model = Game    

    def __init__(self, *args, **kwargs):
        super(GameForm, self).__init__(*args, **kwargs)

        self.fields['publisher'].queryset = Publisher.objects.order_by('name')
        self.fields['platform'].queryset = Platform.objects.order_by('platform')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.