S
S
Salavat Sharapov2014-05-26 14:17:53
Django
Salavat Sharapov, 2014-05-26 14:17:53

ForeignKey for list_filter, how?

The project uses two admin panels (admin panel for users + admin panel for users ( AY ))
In AY, a model of the type is used:

class Document(model.Models):
    title = models.CharField(_('title'), max_length=500)
    file = models.FileField(_('file'), upload_to='documents')
    order = models.IntegerField(_('order'), default=0)
    dtype = models.ForeignKey(to=DocType, blank=True, null=True)

class DocType(model.Models):
    title = models.CharField(_('title'), max_length=500)
    order= models.SmallIntegerField(verbose_name=u"Вес")

To isolate the display in AU, in the DocType selection field in admin.py there is a method:
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        field = super(DocumentAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
        if db_field.name == 'title' or db_field.name == 'order' or db_field.name == 'dtype':
            if request.АЮ_site is not None:
                    field.queryset = field.queryset.filter(АЮ_site=request.АЮ_site)
            else:
                    field.queryset = field.queryset.none()
        return field

But if filter_list=['dtype'] is added to admin.py for the DocumentAdmin class , then all Doctypes (not only those created by these users) are displayed in the list.
How to be?
Predefine your filter for each case, example HERE ?
ps dj-1.6

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Salavat Sharapov, 2014-06-05
@desperadik

I wrote my own filter.

class TypeFilter(SimpleListFilter):
    title = u'Тип документа'
    parameter_name = 'dtype'

    def lookups(self, request, model_admin):
        records = DocType.objects.all().filter(user_site=request.user_site)
        list_1 = []
        list_2 = []
        for data in records:
            list_1.append(data.title)
        for f in map(lambda x, y: (x, y), iter(list_1), iter(list_1)):
            list_2.append(f)
        return tuple(list_2)

    def queryset(self, request, queryset):
        records = DocType.objects.filter(user_site=request.user_site)
        for data in records:
            if self.value() == data.title:
                records = queryset.filter(dtype=data)
                return records

class DocumentAdmin(ModelAdmin)
      list_filter = (TypeFilter, )

M
mefisto, 2014-05-26
@mefisto

It seems to me that you need to google in the direction of row level permissions
or as an example here - django-guardian.readthedocs.org/en/v1.2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question