Django-TastyPie-ForeignKey-full=true不工作

Django-TastyPie-ForeignKey-full=true不工作,django,tastypie,Django,Tastypie,我有两个模型产品和CartItem: #product models.py class Product(models.Model): objects = ProductManger() name = models.CharField(max_length=200) brand_name = models.CharField(max_length=100) description = models.TextField() #CartItem models.py class

我有两个模型产品和CartItem:

#product models.py
class Product(models.Model):
   objects = ProductManger()
   name = models.CharField(max_length=200)
   brand_name = models.CharField(max_length=100)
   description = models.TextField()

#CartItem models.py
class CartItem(models.Model):
   objects = CartItemManager()
   cart = models.ForeignKey(Cart)
   product = models.ForeignKey(Product)
   quantity = models.PositiveSmallIntegerField(blank=True, null=True, default=1)
我想获取一个购物车的所有cartitems,在api.py(tastypi)中,我有以下内容:

class CartItemRelatedResource(ModelResource):

    class Meta:
       queryset = Product.objects.all()
       resource_name = 'item_product'
       allowed_methods = ['get'] 
       include_resource_uri = False       
       authentication = SessionAuthentication()

class CartItemResource(ModelResource):
     product = fields.ForeignKey(CartItemRelatedResource, 'product', full=True)

    class Meta:
       queryset = CartItem.objects.all()
       resource_name = 'cart_item'
       excludes = ['modification_date']
       allowed_methods = ['post', 'get', 'delete']
       authentication = SessionAuthentication()

    def get_cart_items(self, request, **kwargs):
       self.method_check(request, allowed=['get'])
       self.is_authenticated(request)
       cart_id = request.GET.get('id', '')
       items = CartItem.objects.filter(cart__exact = cart_id)
       data = serializers.serialize('json', items)
       return HttpResponse(data, mimetype='application/json')

但是当我得到商品时,响应中有商品的主键,没有商品名称或描述。我也想在回复中得到产品名称。据我所知,full=true是最好的解决方案(请求最少,因为一个购物车可以有多个购物车项目)。

您版本的get\u cart\u items()没有使用Tastypie代码获取,这就是full=true不起作用的原因


full=True
如果您使用Tastypie默认处理程序来获取数据,它将起作用。您不需要编写自己的get\u cart\u items()

谢谢你的回复。我应该使用
def obj_get_list(self,bundle,**kwargs):cart_id=request.get.get('cart_id')我不完全理解您试图做什么,但在一般情况下,您根本不需要编写任何代码,只需使用默认的tastypie url。我希望url像/api/cart\u item/cart/?id=1,而不是/api/cart\u item/cart/?cart\u id=id。我将尝试使用后一个url,如果它有效,我将接受您的答案。谢谢你的帮助。我是django tastypie的新手:)哦,我现在明白了。如果你在回答中加上这一点,会有帮助的。最好是跳过check_filters()并构建_filters()函数来允许您的过滤器。