如何在Django RestFramework中通过URL过滤嵌套列表

时间:2018-07-19 00:54:57

标签: python django django-rest-framework

我有一个课程模型,并且有一个类别列表……我希望能够过滤类别 这是我的涂胶剂

class CourseSerial(serializers.ModelSerializer):
    categories =  serializers.SerializerMethodField(read_only=True)
    image = VersatileImageFieldSerializer(
        sizes=[
            ('full_size', 'url'),
            ('thumbnail', 'thumbnail__100x100'),
            ('medium_square_crop', 'crop__400x400'),
            ('small_square_crop', 'crop__50x50')
        ]
    )
    class Meta:
        model = Course
        fields = ('id','name_a','name_e','short_desc_a', 'short_desc_e','image','categories') 

    def get_categories(self, obj):
          cats = CourseCategories.objects.filter(course=obj.id)
          return CourseCategoriesSerial(cats,many=True).

class CourseCategoriesSerial(serializers.ModelSerializer):
    class Meta:
        model = CourseCategories
        fields = '__all__'

这是结果

            "active": 1,
            "creation_date": "2018-02-24",
            "creation_user": 1,
            "categories": [
                {
                    "id": 1,
                    "active": 1,
                    "creation_date": "2018-02-24",
                    "course": 1,
                    "category": 140,
                    "subcategory": 159,
                    "creation_user": 1
                },

我有一个课程类别模型,它嵌套在课程序列化程序中...我的models.py如下:

class Course(models.Model):

    name_a = models.CharField(max_length=100)
    name_e = models.CharField(max_length=100)
    active = models.IntegerField(blank=True, null=True)
    creation_date = models.DateField(blank=True, null=True)
    creation_user = models.ForeignKey('User', models.DO_NOTHING, db_column='creation_user')

    class Meta:
        managed = True
        db_table = 'course'


class CourseCategories(models.Model):
    course = models.ForeignKey(Course, models.DO_NOTHING)
    category = models.ForeignKey('Lookups', models.DO_NOTHING,related_name='course_category')
    subcategory = models.ForeignKey('Lookups', models.DO_NOTHING,related_name='course_sub_category')
    active = models.IntegerField(blank=True, null=True)
    creation_date = models.DateField(blank=True, null=True

    class Meta:
        managed = True
        db_table = 'course_categories'

我希望能够过滤嵌套列表,URL过滤器。如api/course/?categories__category=140

这是我的观点:

class CourseApiView(generics.GenericAPIView,
                    mixins.ListModelMixin,
                    mixins.CreateModelMixin,
                    mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.DestroyModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseSerial
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    ordering_fields = ('id','popularity','offered','creation_date')
    filter_fields = ('id','name_a','name_e','short_desc_a', 'short_desc_e','level','price','currency','start_date','end_date','details','language','location','priority','status','visible','image','total_rating','total_raters','popularity','offered','offer_value','active','creation_date','creation_user')
    lookup_field = 'id'
    pagination_class = LargeResultsSetPagination

1 个答案:

答案 0 :(得分:1)

您可以使用以下任何软件包,

1。 django-url-filter

2。 django-filter

我认为,第一个更适合您的要求。 Follow the instructions,您将获得要搜索的内容