类别子类别未正确显示 drf django

huangapple go评论95阅读模式
英文:

category subcategory not showing properly drf django

问题

I have this category models:

  1. class Category(MPTTModel):
  2. name = models.CharField(max_length=100)
  3. slug = models.SlugField()
  4. parent = TreeForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE, db_index=True)
  5. class MPTTMeta:
  6. unique_together = ('slug', 'parent')
  7. verbose_name_plural = "categories"
  8. order_insertion_by = ['name']
  9. def __str__(self):
  10. full_path = [self.name]
  11. k = self.parent
  12. while k is not None:
  13. full_path.append(k.name)
  14. k = k.parent
  15. return ' -> '.join(full_path[::-1])

Serializers.py:

  1. # category Listing Api
  2. class CategorySerializers(serializers.ModelSerializer):
  3. class Meta:
  4. model = Category
  5. fields = ('id', 'name', 'slug', 'children')
  6. def get_fields(self):
  7. fields = super().get_fields()
  8. fields['children'] = CategorySerializer(many=True, read_only=True)
  9. return fields

Views.py:

  1. class CategoryView(generics.ListCreateAPIView):
  2. queryset = Category.objects.filter(parent__isnull=True)
  3. serializer_class = CategorySerializers

Currently, it's working, but I want to show subcategories inside categories only, not as main categories. As you can see, subcategories are listed inside main categories and with main categories also.

I am expecting this result:

  1. [
  2. {
  3. "id": 1,
  4. "name": "Animals & Wildlife",
  5. "slug": "animals_wildlife",
  6. "children": []
  7. },
  8. {
  9. "id": 2,
  10. "name": "main cat",
  11. "slug": "mai_cat",
  12. "children": [
  13. {
  14. "id": 3,
  15. "name": "sub_cat",
  16. "slug": "submaincat",
  17. "children": []
  18. }
  19. ]
  20. },
  21. {
  22. "id": 4,
  23. "name": "maincat2",
  24. "slug": "maincat2",
  25. "children": [
  26. {
  27. "id": 5,
  28. "name": "submaincat2",
  29. "slug": "submaincat2",
  30. "children": []
  31. }
  32. ]
  33. },
  34. {
  35. "id": 6,
  36. "name": "maincat3",
  37. "slug": "maincat3",
  38. "children": []
  39. }
  40. ]

Please let me know if you need any further assistance.

英文:

i have this category models:

  1. class Category(MPTTModel):
  2. name = models.CharField(max_length = 100)
  3. slug = models.SlugField()
  4. parent = TreeForeignKey('self',blank=True, null=True, related_name='children', on_delete=models.CASCADE, db_index = True)
  5. class MPTTMeta:
  6. unique_together = ('slug', 'parent')
  7. verbose_name_plural = "categories"
  8. order_insertion_by = ['name']
  9. def __str__(self):
  10. full_path = [self.name]
  11. k = self.parent
  12. while k is not None:
  13. full_path.append(k.name)
  14. k = k.parent
  15. return ' -> '.join(full_path[::-1])

serializers.py:

category Listing Api

  1. class CategorySerializers(serializers.ModelSerializer):
  2. class Meta:
  3. model = Category
  4. fields = ('id','name','slug','children')
  5. def get_fields(self):
  6. fields = super().get_fields()
  7. fields['children'] = CategorySerializer(many=True, read_only=True)
  8. return fields

views.py:

  1. class CategoryView(generics.ListCreateAPIView):
  2. queryset = Category.objects.filter(parent__isnull=True)
  3. serializer_class = CategorySerializers

currently its working but i want to show subcategory inside category only not as a main category as you can see subcategory are listed inside main category and with maincategory also

类别子类别未正确显示 drf django

I am expecting this result:

  1. [
  2. {
  3. "id": 1,
  4. "name": "Animals & Wildlife",
  5. "slug": "animals_wildlife",
  6. "children": []
  7. },
  8. {
  9. "id": 2,
  10. "name": "main cat",
  11. "slug": "mai_cat",
  12. "children": [
  13. {
  14. "id": 3,
  15. "name": "sub_cat",
  16. "slug": "submaincat",
  17. "children": []
  18. }
  19. ]
  20. },
  21. {
  22. "id": 4,
  23. "name": "maincat2",
  24. "slug": "maincat2",
  25. "children": [
  26. {
  27. "id": 5,
  28. "name": "submaincat2",
  29. "slug": "submaincat2",
  30. "children": []
  31. }
  32. ]
  33. },
  34. {
  35. "id": 6,
  36. "name": "maincat3",
  37. "slug": "maincat3",
  38. "children": []
  39. }
  40. ]

...............................................

答案1

得分: 1

将您的视图集更改为

  1. class CategoryViewSet(ModelViewSet):
  2. queryset = Category.objects.filter(parent__isnull=True)
  3. serializer_class = CategorySerializer

和序列化器更改为

  1. class CategorySerializer(serializers.ModelSerializer):
  2. class Meta:
  3. model = Category
  4. fields = ('id', 'name', 'slug')
  5. def get_fields(self):
  6. fields = super().get_fields()
  7. fields['children'] = CategorySerializer(many=True, read_only=True)
  8. return fields
英文:

Change your viewset to

  1. class CategoryViewSet(ModelViewSet):
  2. queryset = Category.objects.filter(parent__isnull=True)
  3. serializer_class = CategorySerializer

and serializer to

  1. class CategorySerializer(serializers.ModelSerializer):
  2. class Meta:
  3. model = Category
  4. fields = ('id', 'name', 'slug')
  5. def get_fields(self):
  6. fields = super().get_fields()
  7. fields['children'] = CategorySerializer(many=True, read_only=True)
  8. return fields

huangapple
  • 本文由 发表于 2020年1月3日 16:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定