Answer the question
In order to leave comments, you need to log in
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"Вес")
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
Answer the question
In order to leave comments, you need to log in
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, )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question