我有两个型号Product和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(tastypie)我有以下内容:
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')
但是当我get_cart_items时,响应具有产品的pk,它没有产品名称或描述。我想在响应中获得产品名称。从我所读的full = true是最好的解决方案(最小的请求,因为购物车可以有多个购物车项目)。
答案 0 :(得分:1)
你的get_cart_items()版本没有使用Tastypie代码来获取,这就是为什么full = True无效。
如果您使用Tastypie默认处理程序来获取数据, full=True
将起作用。您不需要编写自己的get_cart_items()。