原始异常文本为:'QuerySet' 对象没有属性 'client'
Original exception text was: 'QuerySet' object has no attribute 'client'
尝试在序列化程序 ClientSerializer
上获取字段 client
的值时,我得到了 AttributeError
。
序列化程序字段的命名可能不正确,并且与 QuerySet
实例上的任何属性或键都不匹配。
models.py
class Box(models.Model):
box = models.IntegerField()
controller = models.ForeignKey(Controller, related_name='boxes', on_delete=models.CASCADE)
def __str__(self):
return str(self.box)
class Client(models.Model):
client = models.CharField(max_length=30)
cpf = models.IntegerField()
box = models.OneToOneField(
Box,
on_delete=models.CASCADE,
primary_key=True
)
def __str__(self):
return self.client
serializers.py
class ClientSerializer(serializers.ModelSerializer):
class Meta:
model = Client
fields = [
"id",
"client",
"cpf",
"box",
]
class BoxSerializer(serializers.ModelSerializer):
class Meta:
model = Box
fields = [
"id",
"box",
"controller"
]
views.py
class ClientViewSet(viewsets.ModelViewSet):
serializer_class = ClientSerializer
queryset = Client.objects.all()
def list(self, request, store_pk=None, locker_pk=None, controller_pk=None, box_pk=None):
queryset = Client.objects.filter(box=box_pk, box__controller=controller_pk, box__controller__locker=locker_pk, box__controller__locker__store=store_pk)
serializer = ClientSerializer(queryset, context={'request': request})
return Response(serializer.data)
def retrieve(self, request, store_pk=None, locker_pk=None, controller_pk=None, box_pk=None):
queryset = Client.objects.filter(box=box_pk, box__controller=controller_pk, box__controller__locker=locker_pk, box__controller__locker__store=store_pk)
client = get_object_or_404(queryset)
serializer = ClientSerializer(client, context={'request': request})
return Response(serializer.data)
我正在尝试在 lockers/1/controllers/1/boxes/1/client/
上获取对象客户端
这是 OneToOneField
与框的关系,它在嵌套路由器中
我已经尝试过使用装饰器 @action
但还是没用。
有人知道为什么找不到正确的对象属性吗?
对于 list
方法,您应该在创建新的序列化程序实例时使用 many=True
参数:
serializer = ClientSerializer(queryset, context={'request': request}, many=True)
在 retrieve
的情况下,只应接收一个对象。而不是
client = get_object_or_404(queryset)
你应该在查询集上调用 first()
、last()
(或者最基本和清楚的 - .get(pk=pk)
)来 检索 只有一个项目来自查询集。那么你应该只执行:
# client is one of elements of your queryset
serializer = ClientSerializer(client, context={'request': request})
尝试在序列化程序 ClientSerializer
上获取字段 client
的值时,我得到了 AttributeError
。
序列化程序字段的命名可能不正确,并且与 QuerySet
实例上的任何属性或键都不匹配。
models.py
class Box(models.Model):
box = models.IntegerField()
controller = models.ForeignKey(Controller, related_name='boxes', on_delete=models.CASCADE)
def __str__(self):
return str(self.box)
class Client(models.Model):
client = models.CharField(max_length=30)
cpf = models.IntegerField()
box = models.OneToOneField(
Box,
on_delete=models.CASCADE,
primary_key=True
)
def __str__(self):
return self.client
serializers.py
class ClientSerializer(serializers.ModelSerializer):
class Meta:
model = Client
fields = [
"id",
"client",
"cpf",
"box",
]
class BoxSerializer(serializers.ModelSerializer):
class Meta:
model = Box
fields = [
"id",
"box",
"controller"
]
views.py
class ClientViewSet(viewsets.ModelViewSet):
serializer_class = ClientSerializer
queryset = Client.objects.all()
def list(self, request, store_pk=None, locker_pk=None, controller_pk=None, box_pk=None):
queryset = Client.objects.filter(box=box_pk, box__controller=controller_pk, box__controller__locker=locker_pk, box__controller__locker__store=store_pk)
serializer = ClientSerializer(queryset, context={'request': request})
return Response(serializer.data)
def retrieve(self, request, store_pk=None, locker_pk=None, controller_pk=None, box_pk=None):
queryset = Client.objects.filter(box=box_pk, box__controller=controller_pk, box__controller__locker=locker_pk, box__controller__locker__store=store_pk)
client = get_object_or_404(queryset)
serializer = ClientSerializer(client, context={'request': request})
return Response(serializer.data)
我正在尝试在 lockers/1/controllers/1/boxes/1/client/
上获取对象客户端
这是 OneToOneField
与框的关系,它在嵌套路由器中
我已经尝试过使用装饰器 @action
但还是没用。
有人知道为什么找不到正确的对象属性吗?
对于 list
方法,您应该在创建新的序列化程序实例时使用 many=True
参数:
serializer = ClientSerializer(queryset, context={'request': request}, many=True)
在 retrieve
的情况下,只应接收一个对象。而不是
client = get_object_or_404(queryset)
你应该在查询集上调用 first()
、last()
(或者最基本和清楚的 - .get(pk=pk)
)来 检索 只有一个项目来自查询集。那么你应该只执行:
# client is one of elements of your queryset
serializer = ClientSerializer(client, context={'request': request})