2011-12-23 16 views
3

のは、我々はこれらのモデルを持っていると仮定しましょう、元のプロジェクトは異なりますが、これは一般的なタスクのようになります。tasytpie API経由で商品をカートに入れる方法は?

class Cart(models.Model): 
    owner = models.ForeignKey(User) 
    products = models.ManyToManyField(Product, symmetrical=False) 

class Product(models.Model): 
    title = models.CharField(max_length="255") 
    description = models.TextField() 

は、今私は、APIを経由してカートに製品を載せていきたいと思います。

私はこのように始まっ:

class CartResource(ModelResource): 
    products = fields.ManyToManyField(ProductResource, 'products', full=True) 

    def override_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/product/(?P<prodcut_id>\w[\w/-]*)/$" % (self._meta.resource_name), self.wrap_view('dispatch_detail_product'), name="api_dispatch_detail_product"), 
     ] 

    def dispatch_detail_product(.....): 
     # A get is not useful or is it? 
     # A post could put a product into the cart 
     # A put (preferred) could put a product in the cart 
     # A delete could delete a product from the cart 

    class Meta: 
     queryset = Product.objects.all() 
     authentication = MyBasicAuthentication() 
     authorization = DjangoAuthorization() 
     list_allowed_methods = ['get'] 
     detail_allowed_methods = ['get', 'put', 'delete'] 

    def obj_update(self, bundle, request=None, **kwargs): 
     return super(PrivateSpaceResource, self).obj_create(bundle, request, owner=request.user) 

    def apply_authorization_limits(self, request, object_list): 
     if len(object_list.filter(owner=request.user)) == 0: 
      Cart.objects.create(owner=request.user) 
     return object_list.filter(owner=request.user) 

しかし、私は何をすべきかわかりません。 djangoと比べて、tastypieは絶対に開発者にとって不親切です。

答えて

0

私は関係リソースを作成する必要があると思います。 Plsは以下のコードをチェックします。

class LikeResource(ModelResource): 
    profile = fields.ToOneField(ProfileResource, 'profile',full=True) 
    post = fields.ToOneField(PostResource,'post') 

    class Meta: 
     queryset = Like.objects.all() 
     authentication = ApiKeyAuthentication() 
     authorization = DjangoAuthorization() 
     resource_name = 'like' 
     filtering = { 
      'post': 'exact', 
      'profile':'exact', 
     } 

次にあなたがカートに新製品を追加し、そのリソースへのPOST要求を行うことができます。

+0

私はこの問題を解決済みとマークしますが、テストしませんでした。睾丸を使用しませんでした。 – seb

関連する問題