用Tastypie公开模型方法 [英] Exposing model method with Tastypie
问题描述
我似乎没有办法解决如何使用Tastypie在我的模型中公开一个函数。
例如,我有这个模型:
类游戏(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 )
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)
客户端= 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)
返回结果
我想通过API公开使用口味。
我查看了文档和食谱,但似乎找不到我正在寻找的东西。
任何帮助将不胜感激:)
在你的游戏你可以随时添加可以公开方法的新网址。例如(根据@BigglesZX的评论编辑):来自tastypie的tastypie.resources import ModelResource
的
utils import trailing_slash
class GameResource(ModelResource):
class Meta:
queryset = Game.objects.all()
resource_name ='store'
def prepend_urls(self):
将以下数组的url添加到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对于game.start方法
#你可以做一个方法检查以避免不好的请求
self.method_check(request,allowed = ['get'])
#create self.get_cached_obj_get的基本bundle对象。
basic_bundle = self.build_bundle(request = request)
#使用url中定义的主键,获取游戏
game = self.cached_obj_get(
bundle = basic_bundle,
** self.remove_api_resource_names(kwargs))
#返回什么方法输出,tastypie将处理序列化
return self.create_response(request,game.start ())
所以现在你可以使用以下uri来调用这个方法/ game / [pk] / start /
所以/ game / 1 / start /将调用游戏的开始方法,pk = 1
I am currently working on implementing an API into my Django project and Tastypie seemed like it would be most suitable.
What I can't seem to work out is how to expose a function within my model using Tastypie.
For example, I have this model:
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")
And within this model, I have functions such as this:
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
which I would like to expose through the API using tastypie.
I've looked through the documentation and the cookbook but I can't seem to find what I am looking for.
Any help would be appreciated :)
Within your Game-Resource, you can always prepend new urls that can expose methods. For example (Edited according to the comment by @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())
So now you can call this method using the following uri "/game/[pk]/start/" So "/game/1/start/" will call the start method of game with pk = 1
这篇关于用Tastypie公开模型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!