【问题标题】:Could not resolve URL for hyperlinked relationship无法解析超链接关系的 URL
【发布时间】:2014-05-19 10:16:00
【问题描述】:

我尝试在 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')

序列化器.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/ 处的异常

无法使用视图名称解析超链接关系的 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",
        },
    ]
},

【问题讨论】:

    标签: python django django-models django-rest-framework


    【解决方案1】:

    您的查找字段指向数据库中不存在的article_slug。实际上应该是item_slug。这似乎是导致错误的原因。

    【讨论】:

      【解决方案2】:

      在您的 urls.py 中更改 articleitem 详细信息名称


      试着改成这个

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

      它解决了我的问题。

      【讨论】:

      • 谢谢,帮了大忙
      猜你喜欢
      • 2018-08-27
      • 2019-08-20
      • 2015-12-25
      • 2019-09-15
      • 2020-07-30
      • 2021-07-10
      • 2021-01-25
      相关资源
      最近更新 更多