按类别获取图像django drf

时间:2019-11-29 10:40:15

标签: django django-rest-framework

一切正常,但我想根据类别获取图像。

models.py:

class Image(models.Model):   
      title = models.CharField(max_length = 100)
      image = models.ImageField(upload_to = 'home/tboss/Desktop/image' , default = 'home/tboss/Desktop/image/logo.png')
      category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
      image_keyword = models.TextField(max_length=1000)

def __str__(self):
    return self.title

serializers.py:

class ImageSerializer(serializers.ModelSerializer):
      class Meta:
            model = Image
            fields = ('title','category','image')

views.py:

class ImageView(generics.ListCreateAPIView):
      authentication_classes = []
      permission_classes = []
      pagination_class = None    
      serializer_class = ImageSerializer
      def get_queryset(self):
          cat = int(self.request.query_params['category'])               
          return Image.objects.all().filter(category = cat)

urls.py:

path('image/', views.ImageView.as_view(), name = 'category_image')

当前其工作方式如下:

  localhost:8000/image?category=<category_id> 

没有斜杠

我要实现添加带斜线的类别参数的方法:

path('image/<category>/', views.ImageView.as_view(), name = 'category_image')

我无法在get_query函数中传递类别参数任何建议

0 个答案:

没有答案