문제

I have a custom made content type in FeinCMS.

class DownloadsContent(models.Model):
    title = models.CharField(max_length=200, verbose_name=_('title'))
    files = FileManyToMany(verbose_name=_('files'))

The 'files' field is an manytomany which only shows .doc and .pdf files:

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)

Untill now everything works fine. When adding this content type it shows all files.

But how can I make use of the FilteredSelectMultiple widget in my content type? Like:

enter image description here

도움이 되었습니까?

해결책 2

In my own model field class, FileManyToMany, Add "def formfield(self, ...)" which adds the widget

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)

다른 팁

Actually an easier way of achieving this would be:

class DownloadContentInline(FeinCMSInline):
    filter_horizontal = ['files']


class DownloadContent(models.Model):
    feincms_item_editor_inline = DownloadContentInline
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top