Django Grappelli 在内联管理上添加自动完成查找
Django Grapelli add autocomplete lookups on InlineAdmin
我有这 3 个模型:
class MyFile(models.Model):
file = models.FileField(upload_to="files/%Y/%m/%d")
def __unicode__(self):
"""."""
return "%s" % (
self.file.name)
class ExampleModel(models.Model):
attached_files =models.ManyToManyField(MyFile)
main_model = models.ForeignKey(MainModel)
class MainModel(models.Model):
attached_files =models.ManyToManyField(MyFile)
和我的admin.py
如下:
class ExampleModelAdminInline(admin.TabularInline):
model = ExampleModel
extra = 2
class MainModelAdmin(admin.ModelAdmin):
inlines = [ExampleModelAdminInline]
我使用 django-grapelli
是因为它为多对多字段提供自动完成查找。但是,我不确定如何将此自动完成查找添加到 TabularInline
管理员。任何人都可以向我解释如何设置 attached_files
字段以进行自动完成查找吗?
首先,您需要在要搜索的 Model
中设置静态方法 autocomplete_search_fields()
,在您的例子中是 MyFile
。从 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
class,考虑到它的相关 Model
(例如,您的 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)
您还可以检查 问题以更好地理解。
我有这 3 个模型:
class MyFile(models.Model):
file = models.FileField(upload_to="files/%Y/%m/%d")
def __unicode__(self):
"""."""
return "%s" % (
self.file.name)
class ExampleModel(models.Model):
attached_files =models.ManyToManyField(MyFile)
main_model = models.ForeignKey(MainModel)
class MainModel(models.Model):
attached_files =models.ManyToManyField(MyFile)
和我的admin.py
如下:
class ExampleModelAdminInline(admin.TabularInline):
model = ExampleModel
extra = 2
class MainModelAdmin(admin.ModelAdmin):
inlines = [ExampleModelAdminInline]
我使用 django-grapelli
是因为它为多对多字段提供自动完成查找。但是,我不确定如何将此自动完成查找添加到 TabularInline
管理员。任何人都可以向我解释如何设置 attached_files
字段以进行自动完成查找吗?
首先,您需要在要搜索的 Model
中设置静态方法 autocomplete_search_fields()
,在您的例子中是 MyFile
。从 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
class,考虑到它的相关 Model
(例如,您的 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)
您还可以检查