2

The models are

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

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

There is a possibility of using drf-extensions, how it can be used.

How to do setup to have URLs like

/api/article/92285/categories

It should be able to serve responses for GET, POST, and PUT

from following format

{
  "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": [

  ]
}

--
bobsr
  • 3,879
  • 8
  • 30
  • 37

1 Answers1

0

This is an overview how things work in DRF, but if you are new I recommend to go through the official tutorial. I can't mention all details here. It's whole tutorial what are you asking for:

serializers.py

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

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

    class Meta:
        model = Article

views.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

urls.py

url(r'article/(?P<id>\d+)/categories$', ArticleUpdateApiView.as_view())
geckon
  • 8,316
  • 4
  • 35
  • 59
Dhia
  • 10,119
  • 11
  • 58
  • 69
  • the base API is working, would like to setup nested urls like /api/article/92285/categories – bobsr Dec 21 '15 at 13:22
  • In this case you should override the default method implementation of each class you use , like I did with UpdateAPIView. I updated the code. – Dhia Dec 21 '15 at 13:28