Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working in django 4 #35

Open
shahriar350 opened this issue Dec 27, 2022 · 1 comment
Open

Not working in django 4 #35

shahriar350 opened this issue Dec 27, 2022 · 1 comment

Comments

@shahriar350
Copy link

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???

@localveggietable
Copy link

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants