Django Grappelli自动完成M2M [英] Django Grappelli Autocomplete M2M
本文介绍了Django Grappelli自动完成M2M的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直紧随文档,无法在Grappelli中使用M2M自动完成查找.
I followed the docs down to the letter and can't get the M2M autocomplete lookup to work in Grappelli.
#models.py
#main model
class Entry(models.Model):
title = models.CharField(max_length=60)
content = models.TextField()
keywords = models.ManyToManyField(Keyword, blank=True)
#model I want to be searched through while typing in the autocomplete field
class Keyword(models.Model):
name = models.CharField(max_length=30)
@staticmethod
def autocomplete_search_field():
return ('id__iexact', 'name__icontains',)
def __str__(self):
return '%s' % (self.name)
然后在admin.py
中输入:
class EntryAdmin(admin.ModelAdmin):
raw_id_fields = ('keywords',)
autocomplete_lookup_fields = {
'm2m': ['keywords'],
}
class KeywordAdmin(admin.ModelAdmin):
pass
admin.site.register(Entry, EntryAdmin)
admin.site.register(Keyword, KeywordAdmin)
该图片显示即使有keyword
标题为'Finances'的条目也不会返回任何结果.
Image showing that it isn't returning any results even though there's definitely a keyword
entry titled 'Finances'.
推荐答案
正确的方法名称为autocomplete_search_fields
(复数).在您的关键字模型中,您使用的是autocomplete_search_field
The correct method name is autocomplete_search_fields
, plural. In your Keyword model you're using autocomplete_search_field
这篇关于Django Grappelli自动完成M2M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文