Django queryset for DRF view

Viewed 27

I have model called Category where we can add the categories and the records will be a manytomanyfield to Records Model what can my queries be to display all the categories on one page and the if i click on the particular category i should get only records related to that particular category.

models.py

class Category(models.Model):
    name = models.CharField(max_length=255, blank=True)
    records = models.ManytoManyField(Record)

class Record(models.Model):
    name = models.CharField(max_length=255, blank=True)

views.py

class CategoryListView(ListAPIView):
    def get_queryset(self):
        queryset = ?
        return queryset

class RecordListView(ListAPIView):
    def get_queryset(self):
        queryset = ?
        return queryset

/categories/page/

  • Category 1
  • Category 2
  • Category 3
  • Category 4

/Category/Records/page/ Category 1

  • record 1
  • record 2
  • record 3
0 Answers
Related