Models.py
class AllotmentDocket(models.Model):
sales_order = models.ForeignKey(MaterialRequest, on_delete=models.CASCADE, related_name='allotment_sales')
class MaterialRequest(models.Model):
kit = models.ForeignKey(Kit, on_delete=models.CASCADE, related_name='kit')
quantity = models.IntegerField(default=0)
class Kit(models.Model):
kit_name = models.CharField(max_length=255, default=0)
components_per_kit = models.IntegerField(default=0)
product = models.ManyToManyField(Product, related_name='kit_product')
如何通过相关的AllotmentDocket获取所有产品信息?
我尝试过:
def PrintAllotmentDocket(request, pk):
AlotmentDocket = get_object_or_404(AllotmentDocket, id=pk)
kit = MaterialRequest.objects.filter(id=AlotmentDocket.sales_order).values('kit')
但这会导致错误:
int()参数必须是字符串,类似字节的对象或数字,而不是 'MaterialRequest'
答案 0 :(得分:1)
您可以使用
进行过滤Product.objects.filter(
kit_product__kit__allotment_sales=pk
)
使用pk
的主键AllotmentDocket
(因此将其传递给视图)。
注意:
related_name
是 reverse 中关系的名称,而不是定义方向上的关系。