autocomplete_field is not available if override formfield_for_foreignkey

时间:2019-04-08 12:54:19

标签: django-rest-framework

I'm creating a model admin. The model has a foreign key field. I have specified the queryset by overriding formfield_for_foreignkey method. But now I cannot use the autocomplete_fields as before.

class FooAdmin(ReadOnlyModelAdmin):
    list_display = ('id', 'foreignkey_field', 'created_date')
    autocomplete_fields = ['foreignkey_field']

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'foreignkey_field':
         return Foriegn_keyChoiceField(queryset=model.objects.filter(foriegn_key__approval=True), required=False)
    return super().formfield_for_foreignkey(db_field, request, **kwargs)

class Foriegn_keyChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return "{}".format(obj.name)

0 个答案:

没有答案