我按照接受的答案here更改了Django管理员中外键选择列表中的项目顺序。重新排序很有效,但是在选择列表之后,小添加图标已经消失。我怎样才能找回来?
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)
答案 0 :(得分:2)
您的表单会覆盖管理员应用字段RelatedFieldWidgetWrapper
小部件
为了防止这种情况,应在queryset
方法
__init__
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')