Zexelon
Zexelon

Reputation: 494

Django MPTT tree as model filter in admin

I have a model linked to a related model that is a Django MPTT tree model, I would like to be able to filter the first model using the Django MPTT tree in the admin console.

class Tenders(models.Model):
    ...
    sector=models.ForeignKey(Sector, to_field='sectorId', null=True, blank=True,on_delete=models.CASCADE)
    ...

class Sector(MPTTModel):
    name    = models.CharField(max_length = 255)
    parent  = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True,related_name='children')
    sectorId = models.IntegerField(default=0,null=True,unique=True)

In the Django admin I would like to set up the filters for the Tenders model such that the Django-MPTT tree is the filter.

I have tried using the following:

class adminTenders(admin.ModelAdmin):    
   def linkTo(self,obj):
       return mark_safe("""<a href='{}' target="_blank" >Tender Link</a>""".format(obj.tenderLink))
   linkTo.short_description=''

   list_display=(
    'title',
    'linkTo',
    'sector',
    'region',
    'repository',
    'id',
   )
   list_filter=(
    ('sector', TreeRelatedFieldListFilter),
   )

admin.site.register(Tenders,adminTenders)

However I get the following error when trying to run this and I cant figure it out:

  File "py36/lib/python3.6/site-packages/mptt/admin.py", line 314, in field_choices
    mptt_level_indent * levels_dict[pk])
  KeyError: 0

Any help would be greatly appreciated.

Edit 1: I think I have isolated the issue to the fact that my foreign key in Tenders to Sectors uses a to_field='sectorId instead of the default to link to the pk column. This had to be done for backwards compatibility to an old database scheme that I am stuck with.

Upvotes: 1

Views: 1211

Answers (1)

Zexelon
Zexelon

Reputation: 494

So it turns out this is a bug in the django-mptt code for the field_choices function in the TreeRelatedFieldListFilter class.

To fix it I had to subclass and over ride that function to use the to_field that I had defined.

Here is the custom code:

class TreeRelatedForSectors(TreeRelatedFieldListFilter):
    # Modified from django-mptt code to fix to_field problem
    def field_choices(self, field, request, model_admin):
        mptt_level_indent = getattr(model_admin, 'mptt_level_indent', self.mptt_level_indent)
        language_bidi = get_language_bidi()
        initial_choices = field.get_choices(include_blank=False)
        pks = [pk for pk, val in initial_choices]
        models = field.related_model._default_manager.filter(sectorId__in=pks)
        levels_dict = {model.sectorId: getattr(model, model._mptt_meta.level_attr) for model in models}
        choices = []
        for pk, val in initial_choices:
            padding_style = ' style="padding-%s:%spx"' % (
                'right' if language_bidi else 'left',
                mptt_level_indent * levels_dict[pk])
            choices.append((pk, val, mark_safe(padding_style)))
        return choices

Upvotes: 1

Related Questions