按同一字段的多个值过滤

Filtering by multiple values of the same field

我有以下模型、ViewSet 和 FilterSet。

class Contact(models.Model):
    PERSON = 'PERSON'
    ORGANIZATION = 'ORGANIZATION'
    COMPANY = 'COMPANY'
    TYPES = (
        (PERSON, 'Person'),
        (ORGANIZATION, 'Organization'),
        (COMPANY, 'Company'),
    )
    # irrelevant fields removed
    type = models.CharField(max_length=50, choices=TYPES, null=True)

class ContactViewSet(CustomViewSet):
    model = models.Contact
    read_serializer_class = serializers.ContactReadSerializer
    write_serializer_class = serializers.ContactWriteSerializer
    model_name = 'contact'
    filterset_class = filtersets.ContactFilterSet
    filter_backends = (filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend)
CONTACT_TYPE_CHOICES = (
    'PERSON',
    'COMPANY',
    'ORGANIZATION'
)
class ContactFilterSet(FilterSet):
    # irrelevant fields removed
    type= CharFilter(
        choices=CONTACT_TYPE_CHOICES,
        field_name='type',
        lookup_expr='in'
    )
    class Meta:
        model = models.Contact
        fields = ('related_contacts', 'type', 'subtype', 'archived', 'legal_files', 'events', 'expenses', 'invoices', 'notes')

我想按类型过滤联系人。例如,我希望我的查询响应 type == 'PERSON' or 'COMPANY' 处的所有联系人 理论上,据我了解 django-filter reference,我应该能够使用 /contacts?type=PERSON&type=COMPANY 这样的查询来实现。但是,此 returns 匹配最右边查询的联系人(在本例中 type=COMPANY

我做错了什么?我是不是完全用错了方法?

也许是这个?

type = MultipleChoiceFilter(choices=CONTACT_TYPE_CHOICES)

field_name='type' 也是不必要的,因为它与过滤器字段名称相同。