2012-12-29 18 views
11

के साथ मॉडल विधि का खुलासा करना मैं वर्तमान में अपने डीजेगो प्रोजेक्ट में एपीआई को लागू करने पर काम कर रहा हूं और टेस्टीपी ऐसा लगता है कि यह सबसे उपयुक्त होगा।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") 

और इस मॉडल के भीतर, मैं इस तरह के रूप में कार्य किया है::

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 

जो मैं tastypie का उपयोग कर एपीआई के माध्यम से पर्दाफाश करने के लिए चाहते हैं

उदाहरण के लिए, मैं इस मॉडल की है।

मैंने प्रलेखन और कुकबुक को देखा है, लेकिन मुझे लगता है कि मैं जो खोज रहा हूं उसे नहीं मिल रहा है।

किसी भी मदद की सराहना की जाएगी :)

+2

के साथ गेम की प्रारंभ विधि को कॉल करेगा, शायद आप यह देखना चाहते हैं: https://github.com/gati/tastypie-model-method –

उत्तर

20

अपने खेल-संसाधन के भीतर, आप हमेशा नए यूआरएल है कि तरीकों का खुलासा कर सकते हैं पहले जोड़ें कर सकते हैं। उदाहरण के लिए (@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 का उपयोग कर इस विधि कॉल कर सकते हैं "/ खेल/[पी]/शुरू /" तो "/ खेल/1/शुरू करें/"पीके = 1

+1

' cached_obj_get' फ़ंक्शन के लिए हस्ताक्षर इस उत्तर के अनुसार बदल गया है: http://stackoverflow.com/a/15159600/258794 – BigglesZX

+0

आप सही हैं @ बिगल्सज़एक्स मैंने अपने उत्तर को tastypie के नवीनतम संस्करण को प्रतिबिंबित करने के लिए अपडेट किया है। –

+0

आप tastypie 0.9.11 के साथ कैसे करेंगे? मुझे इस संस्करण में कोई prepend_urls फ़ंक्शन नहीं मिला:/ – aRkadeFR

संबंधित मुद्दे