django-rest-framework-recursive icon indicating copy to clipboard operation
django-rest-framework-recursive copied to clipboard

Not working in django 4

Open shahensaifullah opened this issue 2 years ago • 1 comments

DJango 4.1 djangorestframework==3.14.0

models.py

class Category(PreModel):
    name = models.CharField(max_length=255)
    slug = models.SlugField(blank=True)
    image = models.ImageField(null=True, blank=True)
    parent = models.ForeignKey('self', related_name='get_category_children', on_delete=models.CASCADE, null=True,
                               blank=True)

    def __str__(self):
        return self.name
serializers.py

class CategoryGetSerializer(serializers.ModelSerializer):
    children = RecursiveField(many=True, read_only=True)

    class Meta:
        model = Category
        fields = "__all__"
views.py

class CategoryView(ModelViewSet):
    pass

    def get_serializer_class(self):
        # return CategorySerializer

        if self.action == 'list':
            return CategoryGetSerializer
        else:
            return CategorySerializer

    queryset = Category.objects.filter(parent__isnull=True)
Response json

[
  {
    "id": 0,
    "children": [
      "string"
    ],
    "date_created": "2022-12-27T11:12:28.099Z",
    "date_updated": "2022-12-27T11:12:28.099Z",
    "active": true,
    "deleted_at": "2022-12-27T11:12:28.099Z",
    "name": "string",
    "slug": "4p7XC77HuaJPRhUyaUNGMMpcbLFmeWe0rwyr",
    "image": "string",
    "parent": 0
  }
]

No data is response here in children. but here parent has many ids. I want to all children serializers in children. How can i solve this problem???

shahensaifullah avatar Dec 27 '22 11:12 shahensaifullah

bump

localveggietable avatar Jan 27 '23 13:01 localveggietable