簡體   English   中英

無法解析超鏈接關系的URL

[英]Could not resolve URL for hyperlinked relationship

我嘗試在django-rest-framework上制作API:

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

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

#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')
)

和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'))

當我嘗試獲取項目列表( http://127.0.0.1:8000/article/ )時,出現錯誤

/ article /的例外

無法使用視圖名稱“ articleitem-detail”解析超鏈接關系的URL。 您可能無法在API中包含相關模型,或者在此字段上錯誤地配置了lookup_field屬性。

如何解決這個問題? 我要保存此url結構,同時為每個對象設置url字段:

文章項

{
    "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",
    }
},

文章類別

{
    "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",
        },
    ]
},

您的查找字段指向數據庫中不存在的article_slug 它實際上應該是item_slug 這似乎是導致錯誤的原因。

在您的urls.py中更改商品詳細信息名稱


嘗試將其更改為此

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

它解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM