Ответ 1
В вашем Game-Resource вы всегда можете добавлять новые URL-адреса, которые могут выставлять методы. Например (отредактировано в соответствии с комментарием @BigglesZX):
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
class GameResource(ModelResource):
class Meta:
queryset = Game.objects.all()
resource_name = 'store'
def prepend_urls(self):
""" Add the following array of urls to the GameResource base urls """
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/start%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('start'), name="api_game_start"),
]
def start(self, request, **kwargs):
""" proxy for the game.start method """
# you can do a method check to avoid bad requests
self.method_check(request, allowed=['get'])
# create a basic bundle object for self.get_cached_obj_get.
basic_bundle = self.build_bundle(request=request)
# using the primary key defined in the url, obtain the game
game = self.cached_obj_get(
bundle=basic_bundle,
**self.remove_api_resource_names(kwargs))
# Return what the method output, tastypie will handle the serialization
return self.create_response(request, game.start())
Итак, теперь вы можете вызвать этот метод, используя следующий uri "/game/[pk]/start/" Итак, "/game/1/start/" вызовет метод начала игры с pk = 1