[Answer]-Django-Rest: Reusable application design

1👍

A lot of how your API is designed will depend on how tight the relations are. This isn’t really a REST problem, or a Django problem, but it’s more of a url design problem.

You are currently using nested urls, where each part of the hierarchy is included in the url, even if it isn’t useful or necessary. It is generally recommended that if you don’t need each part of the hierarchy, it shouldn’t be in the url, and your urls will become flat as a result. Heroku’s API design guide is a useful read, though you must consider the impact on your application and whether or not it is worth it.

The Comments do not need to have any information about topic to which this article is attached and yet it has that information.

This is one of the problems with nested designs, you end up with deeply nested structures with a lot of useless information. But what is important to remember is that REST is not at all related to the URL structure of your API, but some people do have strong opinions on how they should work together.

If you were looking to go for a flat approach with your urls, which would remove topic and article from the patterns, you would end up with urls similar to the following:

/topics/{topic_id}/
/articles/?topic={topic_id}
/articles/{article_id}/
/comments/?article={article_id}
/comments/{comment_id}/

This will allow you to remove the need for the topics to be in the url, as well as the article, so only the most important information is visible in the url: the comment information.

Django REST Framework has great filtering support that can be used to make useful, flat APIs.

Leave a comment