gpt4 book ai didi

python - Django Rest Framework,查找字段配置不正确

转载 作者:行者123 更新时间:2023-12-02 20:04:24 25 4
gpt4 key购买 nike

我收到错误

Could not resolve URL for hyperlinked relationship using view name "taxonomy-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.

这是一个 MWE:

urls.py:

from django.contrib import admin
from django.urls import include, path

from rest_framework import routers
from testproj import views

router = routers.DefaultRouter()
router.register(r'taxonomy', views.TaxonomyViewSet)
router.register(r'taxonomy-term', views.TaxonomyTermViewSet)

urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(router.urls))
]

models.py:

from django.db import models

class Taxonomy(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField()

def __str__(self):
return self.name

class Meta:
verbose_name = "Taxonomy"
verbose_name_plural = "Taxonomies"

class TaxonomyTerm(models.Model):
taxonomy = models.ForeignKey(
"Taxonomy",
on_delete=models.CASCADE,
related_name="terms",
related_query_name="term"
)
parent = models.ForeignKey(
"self",
on_delete=models.CASCADE,
related_name="children",
related_query_name="child",
blank=True,
null=True
)
name = models.CharField(max_length=255,)
description = models.TextField()
slug = models.SlugField(unique=True)

def __str__(self):
return self.name

class Meta:
verbose_name = "Term"
verbose_name_plural = "Terms"

序列化器.py:

from testproj.models import TaxonomyTerm, Taxonomy
from rest_framework import serializers

class TaxonomySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Taxonomy
fields = ('name', 'description', 'slug')
lookup_field = 'slug'
extra_kwargs = {
'url': {'lookup_field': 'slug'}
}


class TaxonomyTermSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TaxonomyTerm
fields = ('taxonomy', 'parent', 'name', 'description', 'slug')
lookup_field = 'slug'
extra_kwargs = {
'url': {'lookup_field': 'slug'}
}

views.py

from rest_framework import viewsets
from testproj.models import Taxonomy, TaxonomyTerm
from testproj.serializers import TaxonomySerializer, TaxonomyTermSerializer

class TaxonomyViewSet(viewsets.ModelViewSet):
queryset = Taxonomy.objects.all().order_by('name')
serializer_class = TaxonomySerializer
lookup_field = 'slug'


class TaxonomyTermViewSet(viewsets.ModelViewSet):
queryset = TaxonomyTerm.objects.all().order_by('name')
serializer_class = TaxonomyTermSerializer
lookup_field = 'slug'

这到底是怎么回事?我浏览了很多类似的问题,但我看不出我犯了任何常见的错误。

最佳答案

TaxonomyTermSerializer 不提供 parent 超链接的 lookup_field,也不提供 taxonomy 超链接。

它应该适用于:

class TaxonomyTermSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TaxonomyTerm
fields = ('taxonomy', 'parent', 'name', 'description', 'slug')
lookup_field = 'slug'
extra_kwargs = {
'url': {'lookup_field': 'slug'},
'taxonomy': {'lookup_field': 'slug'},
'parent': {'lookup_field': 'slug'},
}

关于python - Django Rest Framework,查找字段配置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55102004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com