声明自定义表单字段会删除添加图标

时间:2011-12-16 11:28:04

标签: django django-admin django-forms

我按照接受的答案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)

1 个答案:

答案 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')