2012-10-12 51 views
7

我有一個模型,其中有兩個字段是其他模型的外鍵。django-grappelli具有多個外鍵字段的自動完成查找

class Homepage(models.Model): 
    featured_user = models.ForeignKey('auth.user') 
    featured_story = models.ForeignKey('site_stories.story') 

    @staticmethod 
    def autocomplete_search_fields(): 
    return ("featured_user__icontains", "featured_story__icontains",) # Is this right? 


class HomepageAdmin(admin.ModelAdmin): 
    raw_id_fields = ('featured_user', 'featured_story',) 
    autocomplete_lookup_fields = { 
    'fk': ['featured_user'], 
    'fk': ['featured_story'] # <====== What should this be??? 
    } 
admin.site.register(Homepage, HomepageAdmin) 

reading the admin docs後,並試圖幾件事情,很顯然,你字面上需要使用標籤「FK」爲格拉佩利應用自動完成格式化查找到現場。所以...我怎麼能用這個模型來做到這一點,那裏有多個外鍵字段?

回答

11
class HomepageAdmin(admin.ModelAdmin): 
    raw_id_fields = ('featured_user', 'featured_story',) 
    autocomplete_lookup_fields = { 
    'fk': ['featured_user','featured_story'], 
    } 
+0

哦,duhhhhh。 -1對我來說是一個IDIOT。 – TAH

+6

該文件不是很清楚。 –