无法使用视图名称解析超链接关系的URL

时间:2019-04-25 22:58:08

标签: django django-rest-framework

我正在尝试将HyperlinkedModelSerializer应用于我的User_Info模型。当我尝试这样做时,我收到一条错误消息:

无法使用视图名称“ user_info-detail”解析超链接关系的URL。您可能没有在API中包含相关模型,或者在此字段上错误地配置了lookup_field属性。

我在下面提供我的模型,视图,序列化器和URL。可能是我从模型中的AbstractBaseUser继承而来的,而不是models.Model吗?

models.py

class My_User_Info_Manager(BaseUserManager):
    def create_user(self, email, name=None, password=None, phone_number=None, reseller=None):
        user = self.model(
            email = self.normalize_email(email),
            name = name,
            phone_number = phone_number,
            reseller = reseller,

            )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, name=None, password=None):
        user = self.create_user(
            email, name=name, password=password
        )
        user.is_superuser = True
        user.is_staff = True
        user.save(using=self._db)
        return user



class User_Info(AbstractBaseUser):

    email = models.EmailField(max_length=225,
                              unique=True, db_index=True, primary_key=True)

    name = models.CharField(max_length=255, null=False)


    phone_number = models.CharField(max_length=30, null=True)
    reseller = models.CharField(max_length=255, null=True)
    username = "Required for FTP Services"

    ### attributes and methods for superuser to access django admin
    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    def has_perm(self, perm, obj=None):
        return self.is_superuser

    def has_module_perms(self, app_label):
        return self.is_superuser


    objects = My_User_Info_Manager()

    USERNAME_FIELD = 'email'

serializers.py

class UserInfoSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = User_Info # get model

        # add the fields
        fields = '__all__'

views.py

class UserInfoViewSet(viewsets.ModelViewSet):
    """
    Provides `list`, `create`, `retrieve`, `update`, `partial_update` and `destroy` actions for the Users 
    model.

    Overrides 'create' action hash the password from the POST payload.

    The User_Info model is a profile of a seller that can be used to log into the seller side of the web
    app. It contains relevant information about the reseller themselves.
    """

    queryset = User_Info.objects.all()
    serializer_class = UserInfoSerializer
    permission_classes = [IsAuthenticated]

    # override create method to has the password
    def create(self, request):
        request.data['password'] = make_password(request.data['password']) #hash password
        return super().create(request)

urls.py

# urls for ViewSets
router = DefaultRouter()
router.register(r'accounts', accounts_views.AccountsViewSet)
router.register(r'activity', activity_views.ActivityViewSet)
router.register(r'customers', customers_views.CustomersViewSet)
router.register(r'users', users_views.UserInfoViewSet)
router.register(r'products', products_views.ProductsViewSet)
router.register(r'payments', payments_views.PaymentsViewSet)

urlpatterns = [
    path('', include(router.urls)),
    path('admin/', admin.site.urls),
    path(r'api-token-auth/', obtain_jwt_token),
    path(r'api-token-refresh/', refresh_jwt_token),
    path('tax/', include('tax.urls')),
    # path('cdr/', include('cdr.urls')),

]

1 个答案:

答案 0 :(得分:1)

您应该在UserInfoSerializer中序列化网址,例如

url = serializers.HyperlinkedRelatedField(
        view_name='user_info-detail', source='user_info', read_only=True)

serializers.py

class UserInfoSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedRelatedField(
    view_name='user_info-detail', source='user_info', read_only=True)

    class Meta:
        model = User_Info # get model

        # add the fields
        fields = '__all__'

在urls.py中,更新:

router.register(r'users', users_views.UserInfoViewSet)

router.register(r'users', users_views.UserInfoViewSet, base_name='user_info')

建议:仅在您的命名约定中,将user_info更改为user或将User_Info更改为User