如何查询过滤器自外键
how to query filter self foreign key
query = Services.objects.filter(parent_id__isnull=True,sub_service__type=0)
当我过滤 sub_service__type=1
它 returns 正确的输出。即 type 1
对应 sub_service
,但是当我将其更改为 sub_service__type=0
时,filters
不起作用。相反,它 returns 我每次输出。即 type 0,1
而不是 type 0
代码如下:
# MODELS
class Services(models.Model):
type_ = ((1, 'INCLUSIVE'),
(0, 'EXCLUSIVE'))
service_id = models.AutoField(primary_key=True)
parent_id = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='sub_service')
type = models.SmallIntegerField(blank=True, null=True, choices=type_)
# VIEWS
@action(detail=False, methods=['get'], permission_classes=(AllowAny,))
def service_list(self, request):
query = Services.objects.filter(parent_id__isnull=True,sub_service__type=0)
serializer = ServiceSerializer(query , many=True).data
return Response(serializer)
# SERIALIZER
class SubServiceSerializer(serializers.ModelSerializer):
class Meta:
model = Services
fields = "__all__"
class ServiceSerializer(serializers.ModelSerializer):
sub_service = SubServiceSerializer(many=True)
class Meta:
model = Services
fields = "__all__"
如果您使用 sub_service__type=1
进行过滤,您会检索 所有 Service
具有 至少 一个相关 Service
与 type=1
。但是因此允许存在其他具有不同类型的相关子Service
。 .sub_service
管理器还将 不 过滤相关对象的查询集。
您也可以使用 Prefetch
object [Django-doc] 来过滤关系:
from django.db.models import <strong>Prefetch</strong>
query = Services.objects.filter(
parent_id__isnull=True,sub_service__type=0
).prefetch_related(<strong>Prefetch(</strong>'sub_service', Service.objects.filter(type=0)<strong>)</strong>)
query = Services.objects.filter(parent_id__isnull=True,sub_service__type=0)
当我过滤 sub_service__type=1
它 returns 正确的输出。即 type 1
对应 sub_service
,但是当我将其更改为 sub_service__type=0
时,filters
不起作用。相反,它 returns 我每次输出。即 type 0,1
而不是 type 0
代码如下:
# MODELS
class Services(models.Model):
type_ = ((1, 'INCLUSIVE'),
(0, 'EXCLUSIVE'))
service_id = models.AutoField(primary_key=True)
parent_id = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='sub_service')
type = models.SmallIntegerField(blank=True, null=True, choices=type_)
# VIEWS
@action(detail=False, methods=['get'], permission_classes=(AllowAny,))
def service_list(self, request):
query = Services.objects.filter(parent_id__isnull=True,sub_service__type=0)
serializer = ServiceSerializer(query , many=True).data
return Response(serializer)
# SERIALIZER
class SubServiceSerializer(serializers.ModelSerializer):
class Meta:
model = Services
fields = "__all__"
class ServiceSerializer(serializers.ModelSerializer):
sub_service = SubServiceSerializer(many=True)
class Meta:
model = Services
fields = "__all__"
如果您使用 sub_service__type=1
进行过滤,您会检索 所有 Service
具有 至少 一个相关 Service
与 type=1
。但是因此允许存在其他具有不同类型的相关子Service
。 .sub_service
管理器还将 不 过滤相关对象的查询集。
您也可以使用 Prefetch
object [Django-doc] 来过滤关系:
from django.db.models import <strong>Prefetch</strong>
query = Services.objects.filter(
parent_id__isnull=True,sub_service__type=0
).prefetch_related(<strong>Prefetch(</strong>'sub_service', Service.objects.filter(type=0)<strong>)</strong>)