2017-06-21 11 views
1

にオートコンプリートの検索を追加します。ただし、このオートコンプリート検索をTabularInline管理者に追加する方法がわかりません。誰も私にattached_filesフィールドを自動補完検索を設定する方法を教えてもらえますか?ジャンゴGrapelliは、私は、この3つのモデルを持っているInlineAdmin

+0

あなたの 'attached_files'のうちの2つを書いているときに' MyFile'からオートコンプリートしますか? – DarkCygnus

+0

はい添付ファイルフィールドのMyFileオブジェクトを検索して、それらのフィールドの参照を設定したいとします。 –

答えて

1

まず、の検索対象Modelに静的メソッドautocomplete_search_fields()を設定する必要があります。 docsから、我々が得る:

class MyFile(models.Model): 
    #your variable declaration... 

    @staticmethod 
    def autocomplete_search_fields(): 
     return ("id__iexact", "name__icontains",) #the fields you want here 

をあなたはまた、同様に、静的メソッドを宣言するのではなく、GRAPPELLI_AUTOCOMPLETE_SEARCH_FIELDSを定義することができます。

GRAPPELLI_AUTOCOMPLETE_SEARCH_FIELDS = { 
    "myapp": { 
     "MyFile": ("id__iexact", "name__icontains",) 
    } 
} 

次に、あなたがあなたの希望adminクラスに検索し、生のフィールドを追加する必要があります(例えば、あなたのExampleModel)がManyToManyFieldのものであることを考慮してください。同様の方法でForeignKeyを処理することもできます。また、前述のドキュメントから:

class ExampleModel(models.Model): 
    main_model = models.ForeignKey(MainModel) #some FK to other Model related 
    attached_files =models.ManyToManyField(MyFile) #the one with static decl  

class MainModelAdmin(admin.ModelAdmin): 
    #your variable declaration... 

    # define raw fields 
    raw_id_fields = ('main_model','attached_files',) 
    # define the autocomplete_lookup_fields 
    autocomplete_lookup_fields = { 
     'fk': ['main_model'], 
     'm2m': ['attached_files'], 
    } 

はこのように、あなたのadmin.siteに関係の両端(あなたのモデル)を登録することを忘れないでください:

#the one with the m2m and the one with the lookup 
admin.site.register(ExampleModel, MainModelAdmin) 

あなたはまた、よりよく理解するためにthis質問を確認することができます。

関連する問題