我在FeinCMS中有自定义内容类型。
class DownloadsContent(models.Model):
title = models.CharField(max_length=200, verbose_name=_('title'))
files = FileManyToMany(verbose_name=_('files'))
'files'字段是一个很多人只显示.doc和.pdf文件:
class FileManyToMany(models.ManyToManyField):
def __init__(self, to=MediaFile, **kwargs):
limit = {'type__in': ['doc', 'pdf']}
limit.update(kwargs.get('limit_choices_to', {}))
kwargs['limit_choices_to'] = limit
super(FileManyToMany, self).__init__(to, **kwargs)
直到现在一切正常。添加此内容类型时,它会显示所有文件。
但是如何在我的内容类型中使用FilteredSelectMultiple小部件?像:
答案 0 :(得分:2)
实际上,实现这一目标的更简单方法是:
class DownloadContentInline(FeinCMSInline):
filter_horizontal = ['files']
class DownloadContent(models.Model):
feincms_item_editor_inline = DownloadContentInline
答案 1 :(得分:0)
在我自己的模型字段类FileManyToMany中,添加“def formfield(self,...)” 添加小部件
from django.db import models
from feincms.module.medialibrary.models import MediaFile
class FileManyToMany(models.ManyToManyField):
def __init__(self, to=MediaFile, **kwargs):
limit = {'type__in': ['doc', 'pdf', 'xls']}
limit.update(kwargs.get('limit_choices_to', {}))
kwargs['limit_choices_to'] = limit
super(FileManyToMany, self).__init__(to, **kwargs)
def formfield(self, **kwargs):
from django.contrib import admin
defaults = {'widget': admin.widgets.FilteredSelectMultiple('vebose_name', False)}
defaults.update(kwargs)
return super(FileManyToMany, self).formfield(**defaults)