简体   繁体   English

无法解析超链接关系的URL

[英]Could not resolve URL for hyperlinked relationship

I try to make API on django-rest-framework: 我尝试在django-rest-framework上制作API:

models.py models.py

class ArticleCategory(SystemModel):
    name =          models.CharField(blank=False, max_length=255)
    category_slug = models.SlugField(blank=True, null=False)
    def save(self, *args, **kwargs):

class ArticleItem(SystemModel):
    name =              models.CharField(blank=False, max_length=255)
    item_slug =         models.SlugField(blank=True, null=False)
    text =              models.TextField(blank=True)
    article_category =  models.ForeignKey('article.ArticleCategory', blank=False, null=False, related_name='article_item_set')

serializers.py serializers.py

class ArticleCategorySerializer(serializers.HyperlinkedModelSerializer):
    article_items = serializers.HyperlinkedIdentityField('article_item_set', view_name='article:item')
    class Meta:
        model =     ArticleCategory
        fields =    ('url', 'name', 'category_slug', 'article_items',)

class ArticleItemSerializer(serializers.HyperlinkedModelSerializer):
    article_category = serializers.HyperlinkedIdentityField('article_category', view_name='article:category')
    class Meta:
        model =     ArticleItem
        fields =    ('url', 'name', 'item_slug', 'text', 'article_category')

urls.py urls.py

#namespace='article'
urlpatterns = patterns('',
   url(r'^(?P<category_slug>[\w-]+)/(?P<item_slug>[\w-]+)', ArticleItemDetail.as_view(), name='item'),
   url(r'^(?P<category_slug>[\w-]+)', ArticleItemListByCategory.as_view(), name='category'),
   url(r'^', ArticleItemList.as_view(), name='item-list')
)

and api.py 和api.py

class ArticleItemDetail(generics.RetrieveUpdateDestroyAPIView):
    model = ArticleItem
    serializer_class = ArticleItemSerializer
    lookup_field = 'article_slug'

class ArticleItemListByCategory(generics.ListAPIView):
    model = ArticleItem
    serializer_class = ArticleItemSerializer
    def get_queryset(self):
        queryset = super(ArticleItemListByCategory, self).get_queryset()
        return queryset.filter(article_category__category_slug=self.kwargs.get('category_slug'))

When i try to get item-list ( http://127.0.0.1:8000/article/ ), i get error 当我尝试获取项目列表( http://127.0.0.1:8000/article/ )时,出现错误

Exception at /article/ / article /的例外

Could not resolve URL for hyperlinked relationship using view name "articleitem-detail". 无法使用视图名称“ articleitem-detail”解析超链接关系的URL。 You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field. 您可能无法在API中包含相关模型,或者在此字段上错误地配置了lookup_field属性。

How to resolve this problem? 如何解决这个问题? I want to save this url-structure and in the same time have url-field for every objects: 我要保存此url结构,同时为每个对象设置url字段:

Article item 文章项

{
    "name": "Article item 1",
    "url": "http://127.0.0.1/article/article-category-1/article-item-1",
    "item_slug": "article-item-1",
    "text": "\u0432\u043e\u043b\u043e\u0432\u043b\u043e",
    "article_category": {
        "name": "Article category 1",
        "url": "http://127.0.0.1/article/article-category-1",
    }
},

Article category 文章类别

{
    "name": "Article category 1",
    "url": "http://127.0.0.1/article/article-category-1",
    "category_slug": "article-category-1",
    "text": "\u0432\u043e\u043b\u043e\u0432\u043b\u043e",
    "article_items": [
        {
            "name": "Article item 1",
            "url": "http://127.0.0.1/article/article-category-1/article-item-1",
        },
        {
            "name": "Article item 2",
            "url": "http://127.0.0.1/article/article-category-1/article-item-2",
        },
    ]
},

Your lookup field is pointing to article_slug that is not present in the database. 您的查找字段指向数据库中不存在的article_slug It should be actually item_slug . 它实际上应该是item_slug This seems to be causing the error. 这似乎是导致错误的原因。

Change the articleitem detail name in your urls.py 在您的urls.py中更改商品详细信息名称


Try changing it to this 尝试将其更改为此

   `url(r'^(?P<category_slug>[\w-]+)/(?P<item_slug>[\w-]+)', ArticleItemDetail.as_view(), name='articleitem-detail'),`

It solved mine. 它解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解析超链接关系的URL - Could not resolve URL for hyperlinked relationship 另一个无法解决 URL 的超链接关系错误 - Another could not resolve URL for hyperlinked relationship error 嵌套序列化程序:无法使用视图名称解析 URL 的超链接关系 - Nested serializers: Could not resolve URL for hyperlinked relationship using view name Django Rest 框架 - 配置不当:无法解析 URL 的超链接关系 - Django Rest Framework - ImproperlyConfigured: Could not resolve URL for hyperlinked relationship Django Rest Framework:无法使用视图名称“post-detail”解析超链接关系的 URL - Django Rest Framework: Could not resolve URL for hyperlinked relationship using view name "post-detail" 错误:无法使用视图名称“ api:products-list”解析超链接关系的URL - Error: Could not resolve URL for hyperlinked relationship using view name “api:products-list” 不断收到“无法使用视图名称“歌曲”为超链接关系解析 URL - Keep getting "Could not resolve URL for hyperlinked relationship using view name "songs" 无法使用视图名称“ source-detail” Django解析超链接关系的URL - Could not resolve URL for hyperlinked relationship using view name “source-detail” Django Django Rest Framework - 无法使用视图名称“user-detail”解析超链接关系的 URL - Django Rest Framework - Could not resolve URL for hyperlinked relationship using view name "user-detail" 无法使用视图名称“book-detail”为超链接关系解析 URL - Could not resolve URL for hyperlinked relationship using view name "book-detail"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM