I am working on a django_filter for data stored in a jsonfield (no, for now I want to keep it this way).
payload = models.JSONField()
I developed a custom filter to emulate the behavior of athe filter for normal fields. I've got it almost working like the "exact"-behavior of a normal field. Only thing is the Choice-Dropdown is empty.
class EventFilter(django_filters.FilterSet):
class Meta:
model = Message
fields = ('event_type', 'origin')
event_type = django_filters.ChoiceFilter(
method='type_filter',
label='type'
)
origin = django_filters.CharFilter(
method='origin_filter',
label='origin'
)
def event_type_filter(self, queryset, name, value):
return queryset.filter(payload__event_type__exact=value)
def origin_filter(self, queryset, name, value):
return queryset.filter(payload__origin__exact=value)
Any ideas how to make it populate the ChoiceFilter?
Update 24.11.22:
Improvements so far (actual filtering does not work but values show up):
I used this filter code:
event_type = django_filters.ModelMultipleChoiceFilter(
label='Event Type',
lookup_expr='exact',
field_name='payload__event_type',
queryset=Message.objects.all().values_list('payload__event_type', flat=True).distinct(),
)
I thought the distinct() would clear duplicate values but probably I am using it wrong... Ideas?