0

I am trying to use a custom filter for a calculated field in admin. I followed the documentation and some other examples and getting this error: Cannot resolve keyword 'kit_frequency' into field. Where did I go wrong?

class KitFrequencyFilter(admin.SimpleListFilter):
    title = 'Frequency'
    parameter_name = 'kit_frequency'

    def lookups(self, request, model_admin):
        return (
            ('Monthly', 'Monthly'),
            ('Bi-Monthly', 'Bi-Monthly'),
            ('Quarterly', 'Quarterly'),
        )
    def queryset(self, request, queryset):
        value = self.value()

        if value == 'Monthly':
            return queryset.filter(kit_frequency='Monthly')
        if value == 'Bi-Monthly':
            return queryset.filter(kit_frequency='Bi-Monthly')
        if value == 'Quarterly':
            return queryset.filter(kit_frequency='Quarterly')
        return  queryset


@admin.register(Contact)
class ContactAdmin(ImportExportModelAdmin):
    resource_class = ContactImportResource

    list_display = ('full_name','program_code','kit_frequency','author','first_name1', 'last_name1', 'first_name2','last_name2','address1','city','province','postal_code','active_status')
    list_filter = ['program_code','author', KitFrequencyFilter]

(simplified) Model:

class Contact(models.Model):
    first_name1 = models.CharField(max_length=100, verbose_name='First Name')
    last_name1 = models.CharField(max_length=100, verbose_name='Last Name', blank=True)
etc...

    def get_kit_frequency(self):
        from programs.models import Program
        kit_frequency = Program.objects.get(program_code=self.program_code).kit_frequency
        return kit_frequency
kit_frequency = property(get_kit_frequency)
DickyS
  • 115
  • 1
  • 10
  • You r `Contact` model doesn't seem to have a kit_frequency field? – HenryM Apr 02 '19 at 17:59
  • I forgot to add kit_frequency = property(get_kit_frequency) to the post, it's at the end of Contact model now. – DickyS Apr 02 '19 at 18:07
  • I think that is the problem (I am not 100% certain). I think `kit_frequency` will not be a database field because it is calculated and therefore won't work in this case. – HenryM Apr 02 '19 at 18:10

0 Answers0