Search code examples
djangotastypie

Tastypie - Should method be in UserResource or ThreadResource?


I'm using django tastypie to build a simple web api. I have two models \ resources: Thread and User I'd like to implement a method that returns all users participating in a thread.

What would be the best practise here, Implementing it in the UserResource or in the ThreadResource?

On the one hand the method requires getting a list of users (which seems to fit the get_object_list method) on the other hand the operation itself queries the thread instances to retrieve its users.

Where should I implement the method?


Solution

  • From your question

    returns all users participating in a thread.

    so basically, your resource is User, hence the method should definitely be in the UserResource class.

    The fact that you are only retrieving users for a certain thread is related to filtering: the operation shouldn't query a thread instance to retrieve its users, but it should take some parameter (such as thread id) and query all the users, filtering by that id.

    Of course, the implementation should be mostly influenced by your app's needs.

    I would also suggest to take a look at nested resources, perhaps this suits your needs better.