我收到这样的错误。
在/ api / users /
处配置不当无法使用视图名称解析超链接关系的URL "用户细节&#34 ;.您可能未能在其中包含相关模型 您的API,或者
lookup_field
属性配置不正确 这个领域。
我读了这个post,但它没有用。
class UserSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='user-detail',
lookup_field='profile')
class Meta:
model = User
fields = ('id', 'username', 'first_name', 'last_name', 'url')
class UserProfileSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source='user.username')
class Meta:
model = UserProfile
fields = "__all__"
# user = serializers.ReadOnlyField(source='owner.username')
def create(self, validated_data):
pass
def update(self, instance, validated_data):
pass
user_profile_list = UserProfileViewSet.as_view({
'get': 'list',
'post': 'create'
})
user_profile_detail = UserProfileViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
})
user_list = UserViewSet.as_view({
'get': 'list'
})
user_detail = UserViewSet.as_view({
'get': 'retrieve'
})
user_profiles_detail = UserViewSet.as_view({
'get': 'profile'
})
router = DefaultRouter()
router.register(r'userprofiles', views.UserProfileViewSet)
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^', include(router.urls))
]
class UserProfileViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
pagination_class = LimitTenPagination
@detail_route(renderer_classes=[renderers.JSONRenderer])
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions
"""
queryset = User.objects.all()
serializer_class = UserSerializer
我的models.py 中的class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
我尝试将user-detail
更改为api:user-detail
(肯定api
名称空间确实存在于主 urls.py 文件中)
答案 0 :(得分:6)
查看文档,我相信您必须使用HyperLinkedRelatedField
。另请参阅this related SO post。
您在序列化程序字段定义中混淆了参数。我认为应该是:
class UserSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedRelatedField(view_name='api:userprofile-detail',
source='profile')
添加了namespace
api
答案 1 :(得分:0)
从lookup_filed
中删除HyperlinkedIdentityField
属性,因为您不需要它。
因此,您的serializer
类应该如下所示:
class UserSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='user-detail', source='profile',)
class Meta:
model = User
fields = ('id', 'username', 'first_name', 'last_name', 'url')
仅当引用具有lookup_filed
作为主键而不是默认relationship-field/attribute
的模型类时,才需要属性autoincrement
。例如,如果您的模型类如下所示,则应该使用它:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile', primary_key=True)
答案 2 :(得分:0)
如果要扩展GenericViewSet和ListModelMixin类,并且在列表视图中添加url字段时出现相同的错误,那是因为没有定义详细信息视图。确保您正在扩展RetrieveModelMixin mixin:
from io import StringIO
txt = """,value
2024-07-07 03:00:00,-0.1
2024-07-07 03:15:00,-0.2
2024-07-07 03:30:00,-0.3
2024-07-07 04:00:00,-0.3
2024-07-07 04:15:00,-0.4
2024-07-07 04:30:00,-0.5
"""
df = pd.read_csv(StringIO(txt), parse_dates=[0], index_col=0)
来自罗文森·加莱戈的回答