首页 > 解决方案 > 在 Django 的 ManyToMany 字段中使用 HyperlinkedModelSerializer

问题描述

设想

在一个项目中,我有以下代码。

模型.py

class Section(models.Model):
    name = models.CharField(_('Section'), max_length=254, unique=True)
    desc = models.TextField(_('Description'), null=True, blank=True)

class Item(CreateUpdateModel):
    name = models.CharField(_('Name'), max_length=254, unique=True)
    price = models.DecimalField(_('Item Price'), max_digits=10, decimal_places=3)
    sections = models.ManyToManyField(Section)
    desc = models.TextField(_('Description'), null=True, blank=True)

序列化程序.py

class ShowSectionSerializer(serializers.HyperlinkedModelSerializer):
    item_set = serializers.HyperlinkedIdentityField(view_name='Show Section Items', lookup_url_kwarg='sections',lookup_field='id')

    class Meta:
        from .models import Section

        model = Section
        fields = ('name', 'id', 'desc', 'item_set')


class ShowItemSerializer(serializers.ModelSerializer):
    sections = ShowSectionSerializer(many=True)

    class Meta:
        from .models import Item
        model = Item
        fields = ('id', 'name', 'sections', 'desc')

网址.py

path('show/item/section/<int:sections>', views.ShowSectionItemView.as_view(), name='Show Section Items'),
path('show/section/', views.ShowSectionView.as_view(), name='Show Sections'),
path('show/item/', views.ShowItemView.as_view(), name='Show-Item'),

视图.py

class ShowItemView(ListAPIView):
    from .models import Item
    from .serializers import ShowItemSerializer
    from django_filters.rest_framework import DjangoFilterBackend
    from rest_framework.filters import SearchFilter
    from .filters import MenuFiltering
    from .paginations import CustomPageSizePagination

    permission_classes = (AllowAny, )
    queryset = Item.objects.all().order_by('id')
    serializer_class = ShowItemSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter)
    pagination_class = CustomPageSizePagination

    filter_class = MenuFiltering
    search_fields = ('^name', '^sections__name')


class ShowSectionItemView(ShowItemView):
    lookup_field = 'sections__id'
    lookup_url_kwarg = 'sections'

    search_fields = ('^name',)

    def get_queryset(self):
        return self.queryset.filter(sections__id=self.kwargs.get('sections'))

class ShowSectionView(ListAPIView):
    from .models import Section
    from .serializers import ShowSectionSerializer
    from django_filters.rest_framework import DjangoFilterBackend
    from .paginations import CustomPageSizePagination
    from .filters import SectionFiltering
    from rest_framework.filters import SearchFilter

    filter_class = SectionFiltering
    pagination_class = CustomPageSizePagination
    permission_classes = (AllowAny, )
    filter_backends = (DjangoFilterBackend, )
    queryset = Section.objects.all().order_by('id')
    serializer_class = ShowSectionSerializer

问题陈述

我希望在每个部分中都有一个 URL,例如:该部分的item_set: https://..../show/item/section/1/在哪里。如果可能的话,URL 会好很多。这将显示特定部分中的所有项目。1Primary Keyitem_set: https://..../show/item?section=1

错误

现在,我正在尝试,https://..../show/item/section/1/但我收到以下错误:

django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "Show Section Items". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

我究竟做错了什么?

标签: pythondjangodjango-rest-framework

解决方案


推荐阅读