2

模型是

class Article(models.Model):
    title = models.CharField(max_length=30)

class Categories(models.Model):
    article = models.ForeignKey(Article)
    name = models.CharField(max_length=30)

有可能使用 drf-extensions,如何使用它。

如何进行设置以拥有类似的 URL

/api/article/92285/categories

它应该能够为 GET、POST 和 PUT 提供响应

从以下格式

{
  "article_id": 92285,
  "views": 0,
  "downloads": 0,
  "shares": 0,
  "handle_url": "",
  "title": "Test dataset",
  "defined_type": "dataset",
  "status": "Drafts",
  "published_date": "",
  "description": "Test description",
  "total_size": 0,
  "owner": {
    "id": 13483,
    "full_name": "John  Carter"
  },
  "authors": [
    {
      "first_name": "John ",
      "last_name": "Carter",
      "id": 13483,
      "full_name": "John Carter"
    }
  ],
  "tags": [

  ],
  "categories": [
  {
    "id": 135,
    "name": "Technology"
  },
  ]


  "files": [

  ]
}

--
4

1 回答 1

0

这是 DRF 中工作原理的概述,但如果您是新手,我建议您阅读官方教程。我不能在这里提及所有细节。这是整个教程你要什么:

序列化程序.py

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Categorie

class ArticleSerializer(serializers.ModelSerializer):
    categories = CategorySerializer(many=True, read_only=true)

    class Meta:
        model = Article

视图.py

class ArticleListCreateApiView(generics.ListCreateAPIView):
    model = Article
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

class ArticleUpdateApiView(generics.UpdateAPIView):
    model = Article
    serializer_class = ArticleSerializer

    def get_object(self):
        boby_data = self.request.data
        id = self.kwargs['id']
        # ... data processing

网址.py

url(r'article/(?P<id>\d+)/categories$', ArticleUpdateApiView.as_view())
于 2015-12-21T13:11:34.403 回答