Django Rest Framework - Could not resolve URL for hyperlinked relationship using view name “user-detail”

前端 未结 17 1240
感动是毒
感动是毒 2020-11-28 03:11

I am building a project in Django Rest Framework where users can login to view their wine cellar. My ModelViewSets were working just fine and all of a sudden I get this frus

相关标签:
17条回答
  • 2020-11-28 03:49

    Bottle = serializers.PrimaryKeyRelatedField(read_only=True)

    read_only allows you to represent the field without having to link it to another view of the model.

    0 讨论(0)
  • 2020-11-28 03:49

    I got that error on DRF 3.7.7 when a slug value was empty (equals to '') in the database.

    0 讨论(0)
  • 2020-11-28 03:51

    This code should work, too.

    class BottleSerializer(serializers.HyperlinkedModelSerializer):
    
      user = UserSerializer()
    
      class Meta:
        model = Bottle
        fields = ('url', 'wine', 'user')
    
    0 讨论(0)
  • 2020-11-28 03:54

    I ran into this same issue and resolved it by adding generics.RetrieveAPIView as a base class to my viewset.

    0 讨论(0)
  • 2020-11-28 03:57

    I was stuck in this error for almost 2 hours:

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

    When I finally get the solution but I don't understand why, so my code is:

    #models.py
    class Users(models.Model):
        id          = models.AutoField(primary_key=True)
        name        = models.CharField(max_length=50, blank=False, null=False)
        email       = models.EmailField(null=False, blank=False) 
        class Meta:
            verbose_name = "Usuario"
            verbose_name_plural = "Usuarios"
    
        def __str__(self):
            return str(self.name)
    
    
    #serializers.py
    class UserSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = Users
            fields = (
                'id',
                'url',
                'name',        
                'email',       
                'description', 
                'active',      
                'age',         
                'some_date',   
                'timestamp',
                )
    #views.py
    class UserViewSet(viewsets.ModelViewSet):
        queryset = Users.objects.all()
        serializer_class = UserSerializer
    
    #urls_api.py
    router = routers.DefaultRouter()
    router.register(r'users',UserViewSet, base_name='users')
    
    urlpatterns = [ 
            url(r'^', include(router.urls)),
    ]
    

    but in my main URLs, it was:

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        #api users
        url(r'^api_users/', include('usersApi.users_urls', namespace='api')),
    
    ]
    

    So to finally I resolve the problem erasing namespace:

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        #api users
        url(r'^api_users/', include('usersApi.users_urls')),
    
    ]
    

    And I finally resolve my problem, so any one can let me know why, bests.

    0 讨论(0)
  • 2020-11-28 04:00

    Today, I got the same error and below changes rescue me.

    Change

    class BottleSerializer(serializers.HyperlinkedModelSerializer):
    

    to:

     class BottleSerializer(serializers.ModelSerializer):
    
    0 讨论(0)
提交回复
热议问题