nullable foreign keys and ModelChoiceFilter

429 views
Skip to first unread message

Michael Rigoni

unread,
Feb 17, 2017, 11:32:56 AM2/17/17
to django-filter
Hello everyone,

I am using django filter on a DRF viewset to filter on a foreign key. This works as expected except when I want to filter entries where the foreign key is null.
I tried creating FilterSet to overcome that limitation:

class ReservationServiceFilter(django_filters.FilterSet):
    service
= django_filters.ModelChoiceFilter(queryset=models.Service.objects.values_list('id', 'name'), null_value='null', null_label='Custom')
   
   
class Meta:
        model
= models.ReservationService
        fields
= ['date', 'service']

But unlike ChoiceFilter, ModelChoiceFilter does not allow null_value and null_label arguments. Looking at the code, it seems only ChoiceFilter accepts it (not even MultipleChoiceFilter).


I was wondering if in fact all filters should accept null_value and null_label arguments as null may be a valid value for most types ? or should it only be implemented for ModelChoiceFilter ? or no other ?

Michael

Carlton Gibson

unread,
Feb 18, 2017, 2:42:59 AM2/18/17
to django...@googlegroups.com
Hi Michael. 

tl;dr Yes. 😃

The null_value and related are new. That's why they end where they do. Absolutely happy to see that logic extended to Multiple and Model variants. 

If you want to work on it open a PR early so you can get some feedback. 

👍🏽

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "django-filter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-filte...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages