Monday, August 31, 2015

Best practice when designing an api (with Django)

Hello;

[ This question might be more about general principles for API design than Django, but since Django is my tool of choice - and my context of understanding, I am posting it here.]

I have been maintaining a small Django based website for some time. It is mostly based on conventional views returning HTML, but both to support JavaScript interactivity and to interact with a traditional desktop application I am gradually creating a REST based API. Up until now I have created the REST endpoints manually, but I now I intend to look into the Django Rest Framework (DRF) and try to do things more properly. Now - the question where I would appreciate some guidance boils down to: "How much should I use my own http based API internally?". Assume I have two django models 'Author' and 'Book', each book can have several authors and of course each author can write several books - i.e. this is a many to many relationship:

    class Author(models.Model):
         ....
         books = models.ManyToManyfield( "Book", ...)

    class Book(models.Model):
         ....

It is then natural(?) to configure the urls:

/books/1       - Get information about the book with ID=1
/authors/7    - Get information about the author with ID=7

But let us say that I wanted to create the endpoint:

/books/7/authors

To get the author information for the authors of the book with ID=7. In my view I have used:

    book = Book.objects.get( pk = 7 )

To get the right book, now I want the author details. As I see it I have two approaches to get the author details:

  1. I can just use the ordinary Model/ORM methods.
  2. I can use my own http api - and "loopback" repeated calls to: /authors/$ID
On the one hand I strongly believe in the "eat your own dogfood" principle, but on the other hand it feels a bit over the top to issue http calls in this situation? Grateful for comments from experienced API desginers.


Joakim

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALKD1M-SQY9JV6vgH_QgqMW6BLh6EAPbTT7KokcQvka8gzp7uQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment