Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用django tastypie添加命令api的最佳方法是什么?_Django_Rest_Tastypie - Fatal编程技术网

使用django tastypie添加命令api的最佳方法是什么?

使用django tastypie添加命令api的最佳方法是什么?,django,rest,tastypie,Django,Rest,Tastypie,我有以下模型,我希望允许用户使用API加入事件 和django tastypie # Conceptual, may not work. class Event(models.Model): title = models.CharField('title', max_length=255) users = models.ForeignKey(User) def join(self, user): self.users.add(user) def

我有以下模型,我希望允许用户使用API加入事件 和django tastypie

# Conceptual, may not work.
class Event(models.Model):
    title = models.CharField('title', max_length=255)
    users = models.ForeignKey(User)

    def join(self, user):
        self.users.add(user)
    def leave(self, user):
        self.users.remove(user)

# join the events with API like...
jQuery.post(
    '/api/v1/events/1/join',
    function(data) {
        // data should be a joined user instance
        // or whatever
        alert(data.username + " has joined.");
    },
);
但我不知道最好的方法。我应该创建
EventJoinResource

# Conceptual, may not work.
class EventJoinResource(Resource):
    action = fields.CharField(attribute='action')

    def post_detail(self, request, **kwargs):
        pk = kwargs.get('pk')
        action = kwargs.get('action')
        instance = Event.objects.get(pk=pk)
        getattr(instance, action)(request.user)

resource = EventJoinResource()

# ??? I don't know how to write this with django-tastypie urls
urlpatterns = patterns('',
    ('r'^api/v1/events/(?P<pk>\d+)/(?P<action>join|leave)/$', include(resource.urls)),
)
#概念性的,可能不起作用。
类EventJoinResource(资源):
action=fields.CharField(attribute='action')
def post_详细信息(自我、请求、**kwargs):
pk=kwargs.get('pk')
action=kwargs.get('action')
instance=Event.objects.get(pk=pk)
getattr(实例、操作)(request.user)
resource=EventJoinResource()
# ??? 我不知道如何用django-tastypie URL写这个
urlpatterns=模式(“”,
('r'^api/v1/events/(?P\d+)/(?Pjoin | leave)/$”,包括(resource.url)),
)

我该怎么办?欢迎您提出任何建议:-)

我认为您可以创建“EventResource”。然后,您可以为用户加入、用户离开和任何其他操作设置不同的事件。因此,基本上,拥有“EventTypeResource”可能会更好

然后,每次事件发生时,您只需发送到“EventResource”,指定事件的类型(通过指定EventTypeResource集合的元素)和任何额外数据,如下所示:

jQuery.ajax ( {
    url : '/api/v1/events/', #note the collection URI not the element URI
    data : {
        type : '/api/v1/event-types/<pk_of_the_event_type', #URI of EventTypeResource
        extra_data : { ... }
    },
    success : function(data) {
        // data should be a joined user instance
        // or whatever
        alert(data.username + " has joined.");
    }
);
jQuery.ajax({
url:“/api/v1/events/”,#注意集合URI而不是元素URI
数据:{

类型:'/api/v1/event types/Sorro我不明白您对
EventTypeResource
的意思。根据您的建议,用户如何加入特定的事件?我是否应该为join传递“join”类型,为leave传递“leave”类型,为更新事件传递“update”类型?在RESTful api中不应该有任何操作或方法调用(这是类似RPC的API的特征)。因此,您应该只创建一个事件,而不是加入它(对其执行操作)。EventTypeResource帮助您区分不同的事件,如加入或离开。因此,是的,在最简单的形式中,EventTypeResource可以只有一个属性“label”或“name”并通过一个ToOneField()与EventResource关联。然后“加入事件的用户”表示为正在创建的EventResource,其关联的EventTypeResource“join”。