2012-12-29 4 views
11

現在、私はDjangoプロジェクトにAPIを実装していますが、Tastypieは最も適しているようです。Tastypieでモデルメソッドを公開する

Tastypieを使用してモデル内で関数を公開する方法はありません。

例えば、私はこのモデルがあります:

class game(models.Model): 
    id = models.AutoField("ID", primary_key=True, editable=False) 
    ip_address = models.OneToOneField(IPAddress, verbose_name="IP Address") 
    port = models.CharField("Port", max_length=5) 
    name = models.CharField("Game Name", max_length=100) 
    ram = models.IntegerField("RAM (mb)", max_length=10) 
    node = models.ForeignKey(node) 
    user = models.ForeignKey(User) 
    config = models.ForeignKey(Config) 
    mysqlserver = models.ForeignKey(MySQLserver) 
    mysqlenabled = models.BooleanField("MySQL Created?") 
    suspended = models.BooleanField("Suspended") 

をし、このモデルの中に、私はこのような機能を持っている:私はtastypieを使用してAPIを介して公開したいと思います

def start(self): 
    config = Config.objects.get(pk=self.config.id) 
    cmds = config.startcmds 
    game = config.gametype 
    parsedcmds = self.replace_variables(cmds) 

    client = phPanel.jelly.jelly.zmqclient(self.ip_address.address) 
    data = {'user':self.generate_username(), 'method':'start_server', 'id':self.id, 'memory':self.ram, 'ip':self.ip_address.address, 
      'port':self.port, 'startcmds':parsedcmds, 'game':game} 

    result = client.send(data) 
    return result 

私はドキュメントと料理本を見てきましたが、私が探しているものが見つからないようです。

任意の助けをいただければ幸いです:)

+2

https://github.com/gati/tastypie-model-method –

答えて

20

あなたのゲーム資源の中で、あなたは常にメソッドを公開することができます新しい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を使用してこのメ​​ソッドを呼び出すことができます "/ゲーム/ [PK]を開始/ /" ので「/ゲーム/ 1/start/"はpk = 1のゲームの開始メソッドを呼び出す

+1

「cached_obj_get」関数のシグネチャこの回答ごとに変更されました:http://stackoverflow.com/a/15159600/258794 – BigglesZX

+0

あなたは正しいです@BigglesZX私はtastypieの最新バージョンを反映するために私の答えを更新しました。 –

+0

あなたはtastypie 0.9.11でどうやってやりますか?私はこのバージョンでは何もprepend_urls関数が見つかりません:/ – aRkadeFR

関連する問題