Home > other >  How to show subcategories under category type
How to show subcategories under category type

Time:06-14

I need to get the child list under the parent list as a group.

class ServiceSerializer(serializers.ModelSerializer):
    cleaning_type = serializers.CharField(source='cleaning_type.cleaning_type_name')
    
    
    class Meta:
        model = Service
        fields = ('id', 'cleaning_type','service_name')


class ServiceTypeViewSet(ModelViewSet):
    serializer_class = ServiceSerializer
    http_method_names = ["get"]
    queryset = Service.objects.all()
    
    def get_queryset(self):
        """
        This view should return a list of all the service types.
        """
        servicename_list = Service.objects.all()
        return servicename_list

It shows:

    [
        {
            "id": 1,
            "cleaning_type": "Lite service",
            "service_name": "Floors",
            
        },
        {
            "id": 2,
            "cleaning_type": "Lite service",
            "service_name": "Bathrooms",
            
        },
        {
            "id": 3,
            "cleaning_type": "Lite service",
            "service_name": "Kitchen",
            
        }
    ]

I want this to be in the following format:

 [
  {
    id: 1,
    cleaning_type: 'Lite service',
    service_name: ['Floors', 'bathroom', 'kitchen'],
  },
  {
    id: 2,
    cleaning_type: 'Moving cleaning',
    service_name: ['Kitchen Including All Appliances And Cabinets'],
  },
]

That means all child elements will be under a separate parent list. Not separate by separate.

models.py is here:

Cleaning Type Model:

class CleaningType(models.Model):
    cleaning_type_name = models.CharField(
        _("Select Cleaning Type"), blank=True, null=True, max_length=255)

    price = models.DecimalField(default=0,max_digits=6, decimal_places=2)    

    def __str__(self):
        return self.cleaning_type_name

Service Model:

class Service(models.Model):

   cleaning_type = models.ForeignKey(
        CleaningType, on_delete=models.CASCADE)
   service_name = models.CharField(
       _("Service Name"), blank=True, null=True, max_length=255)

  

#string type added
def __str__(self):
    return str(self.service_name)
    

I want sub categories under parent caterories. Here cleaning_type is the parent category and service is the child category of cleaning_type. i.e : cleaning_type >> service_type

CodePudding user response:

I'd create a view for the parent category and then get child categories for each parent category. First, you should create a serializer for CleaningType model:

class CleaningTypeSerializer(serializers.ModelSerializer):
    service_types = serializers.SerializerMethodField('get_service_types')

    def get_service_types(self, cleaning_type_name):
        return Service.objects.filter(cleaning_type=cleaning_type_name).values_list("service_name", flat=True)

    class Meta:
        model = CleaningType
        fields = "__all__"

Then, create a view using the new serializer:

class CleaningTypesViewSet(ModelViewSet):
    serializer_class = CleaningTypeSerializer
    http_method_names = ["get"]
    queryset = CleaningType.objects.all()

The response looks something like this:

[
    {
        "id": 1,
        "service_types": [
            "Moving Service 1",
            "Moving Service 2",
            "Moving Service 3"
        ],
        "cleaning_type_name": "Moving Cleaning",
        "price": "200.00"
    },
    {
        "id": 2,
        "service_types": [
            "Another Service 1",
            "Another Service 2"
        ],
        "cleaning_type_name": "Another Cleaning",
        "price": "300.00"
    }
]
  • Related