1

I have a course model and insde it I have a categories list … I want to be able to filter the categories Here's my serilaizers

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__'

and here's the results

            "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
                },

I have a course categories model and it's nested in the course serilaizers...my models.py look like:

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'

I want to be able to filter the nested list, URL filter.. like api/course/?categories__category=140

this my views :

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 Answer 1

1

You could use any of the following packages,

1. django-url-filter

2. django-filter

I think, the first one will more apt for your requirement. Follow the instructions and you will get what you are searching for

Sign up to request clarification or add additional context in comments.

11 Comments

yeah I already use it .. but I couldn't have what I wanted .. because "categories" is not a field of the Course model
if you did it before plz help me because I have a deadline
If you properly configured the django-url-filter package, then, try api/course/?coursecategories=140
can you shoe your views ?
add coursecategories to filter_fields list in your view. Then, try the url I already mentioned
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.