为什么我的主键格式为"12-345.78"'detail: not found'?



当我尝试显示详细信息调用例如/api/products/12-345.67/,我得到detail: Not found.作为回应。如您所见,产品 ID 的格式为12-345.67.我的第一个怀疑是正则表达式验证器(下面列出(,但它的工作方式与它或没有它相同。

模型、序列化程序、视图集和 URL 定义方式如下:

# models.py:
class Product(models.Model):
product_id = models.CharField(max_length=9, primary_key=True)
name = models.CharField(max_length=40)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.product_id
# serializers.py:
class ProductSerializer(serializers.ModelSerializer):
product_id = serializers.RegexField(regex='^d{2}-d{3}.d{2}$', max_length=9, min_length=9, allow_blank=False)
name = serializers.CharField(min_length=6, max_length=50, allow_blank=False)
class Meta:
model = Product
fields = '__all__'
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
lookup_field = 'product_id'
# urls.py:
router = routers.DefaultRouter()
router.register(r'products', ProductViewSet, basename='products')
(...)
urlpatterns = [
path('api/', include(router.urls)),
(...)

我认为您不应该在API端点中使用句号(.(。可能会将id更改为其他内容。& Zapraszamy :D

相关内容

最新更新