在django rest框架中,从不同的类序列化模型
我正在Django编写一个销售点(POS)系统。我有基本的功能,在我添加更多的复杂性之前,我正在尝试清理我的代码 作为这项工作的一部分,我将我的AJAX调用移动到带有django rest框架的API中 在我的模型中,我有一个代表客户购买的所有项目的事务。该交易中的每个单独购买都是一个行项目。在TransactionViewSet api类中,我有一个名为ring_plu的函数。它接受UPC和数量,将该项目作为行项目添加到UPC,然后我希望它返回新的行项目。代码如下所示在django rest框架中,从不同的类序列化模型,django,python-3.x,django-rest-framework,Django,Python 3.x,Django Rest Framework,我正在Django编写一个销售点(POS)系统。我有基本的功能,在我添加更多的复杂性之前,我正在尝试清理我的代码 作为这项工作的一部分,我将我的AJAX调用移动到带有django rest框架的API中 在我的模型中,我有一个代表客户购买的所有项目的事务。该交易中的每个单独购买都是一个行项目。在TransactionViewSet api类中,我有一个名为ring_plu的函数。它接受UPC和数量,将该项目作为行项目添加到UPC,然后我希望它返回新的行项目。代码如下所示 TransactionV
TransactionViewSet
@detail_route(
methods=['post'],
renderer_classes=[renderers.StaticHTMLRenderer]
)
def ring_upc(self, request, *args, **kwargs):
upc = request.POST['upc']
quantity = request.POST['quantity']
if len(upc) != 12:
return Response('Invalid UPC', status=status.HTTP_400_BAD_REQUEST)
grocery = get_object_or_404(Grocery, upc=upc)
transaction = self.get_object()
line_item = transaction.create_line_item(grocery, quantity)
serializer = LineItemSerializer(line_item, context={'request':request})
return Response(serializer.data)
我正在使用POST body{'upc':'071990095314','quantity':1}
调用/api/transactions/10/ring_upc/,并请求应用程序/json响应。我得到的只是这个
urlcodequantityscaledescriptionpricestatustransactionitem
我已经为序列化程序使用了一些不同的选项,但是我似乎无法使序列化程序正常工作
下面是代码中可能有用的其他部分
序列化程序.py
class LineItemSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = LineItem
class LineItem(models.Model):
transaction = models.ForeignKey(Transaction)
code = models.CharField(max_length=30)
quantity = models.DecimalField(max_digits=15, decimal_places=0)
scale = models.DecimalField(max_digits=19, decimal_places=4, null=True)
description = models.CharField(max_length=100)
price = models.DecimalField(max_digits=17, decimal_places=2)
item = models.ForeignKey('inventory.Item')
status = models.CharField(max_length=8, default='ACTIVE')
def __unicode__(self):
return str(self.scale) + ' x ' + self.description + ' ' +\
self.description
def total(self):
return self.price * self.quantity
def cancel(self):
self.status = 'INACTIVE'
型号.py
class LineItemSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = LineItem
class LineItem(models.Model):
transaction = models.ForeignKey(Transaction)
code = models.CharField(max_length=30)
quantity = models.DecimalField(max_digits=15, decimal_places=0)
scale = models.DecimalField(max_digits=19, decimal_places=4, null=True)
description = models.CharField(max_length=100)
price = models.DecimalField(max_digits=17, decimal_places=2)
item = models.ForeignKey('inventory.Item')
status = models.CharField(max_length=8, default='ACTIVE')
def __unicode__(self):
return str(self.scale) + ' x ' + self.description + ' ' +\
self.description
def total(self):
return self.price * self.quantity
def cancel(self):
self.status = 'INACTIVE'
问题出在
渲染器中。StaticHTMLRenderer
从该渲染器可以看到:
一个简单的渲染器,只返回预渲染的HTML。与其他呈现器不同,传递到响应对象的数据应该是表示要返回的内容的字符串
渲染器需要纯文本作为响应数据。然而,它接收dictdict
,并将其转换为字符串,只需对其进行迭代
您只需从
@detail\u route
参数中删除renderer\u classes=[renderers.StaticHTMLRenderer]
。这就是您所有的LineItemSerializer代码吗?是的。这就是crud操作工作和从lineitem API控制器返回串行行项目所需的全部内容。我对此行返回的内容感兴趣:line\u item=transaction.create\u line\u item(杂货店,数量)
,但如果我是你,我会插入一个断点(import ipdb;ipdb.set\u trace()
)在该视图的开头,并在调试器中逐步完成它。通过这种方式,您可能会了解有关数据的更多信息。它似乎是我所期望的行项目。(Pdb)n>/home/david/code/orthosie/register/api_views.py(71)ring_upc()->serializer=lineitemsserializer(line_item,context={'request':request})(Pdb)line_item(Pdb)line_item(Pdb)line_item.ID64(Pdb)line_item.code'071990095314(Pdb)line_item.price Decimal('0.99')(Pdb)反应的状态如何?搞定了。这个问题严重阻碍了这个项目的进展。谢谢你的帮助。