如何过滤get_context_data中的数据

时间:2018-08-03 21:37:27

标签: django

我有2个型号...

scala> val igniteDF = spark.sql("SELECT * FROM person WHERE name = 'Mary Major'")
igniteDF: org.apache.spark.sql.DataFrame = [AGE: int, NAME: string]

scala> igniteDF.count()
java.lang.NoSuchMethodError: org.apache.spark.sql.catalyst.expressions.AttributeReference.<init>(Ljava/lang/String;Lorg/apache/spark/sql/types/DataType;ZLorg/apache/spark/sql/types/Metadata;Lorg/apache/spark/sql/catalyst/expressions/ExprId;Lscala/Option;)V
  at org.apache.ignite.spark.impl.optimization.package$.toAttributeReference(package.scala:144)

一个用于采购订单(PurchaseOrder),一个用于特定采购订单中的项目(PurchaseOrderItem)

我正在尝试在一个TemplateView中访问两个模型的数据...

class PurchaseOrder(models.Model):
    po_number = models.IntegerField(unique=True, default=get_po_number)
    po_date = models.DateField()
    invoice_number = models.ForeignKey(Invoice, on_delete=models.CASCADE)

class PurchaseOrderItem(models.Model):
    po_number_fk = models.ForeignKey(PurchaseOrder, on_delete=models.CASCADE)
    qty = models.IntegerField()
    unit = models.CharField(max_length=100, blank=True, null=True)
    description = models.CharField(max_length=255)
    unit_price = models.DecimalField(max_digits=6, decimal_places=2)
    amount = models.DecimalField(max_digits=6, decimal_places=2)

但是我不确定如何根据通过URL传递的pk过滤purchase_order.pk,也不确定如何根据purchase_order.pk过滤items.po_number_fk。我知道我需要将登录名放入我的PurchaseOrder.objects.get()和PurchaseOrderItem.objects.filter()中,但不确定逻辑是什么

感谢您可以提供的帮助

1 个答案:

答案 0 :(得分:1)

您应该将user = models.ForeignKey(User, on_delete=models.CASCADE)添加到PurchaseOrder模型中,否则您将无法根据用户进行过滤,并且任何已登录的用户都可以看到任何其他用户的订单。同样,如果我是您,我会将po_number_fk重命名为orderpurchase_order,这样就更明显了。

您可以通过pk来访问self.kwargs['pk']字段,并通过以下方式过滤两个模型(如果您进行了上述所有操作):

class PurchaseOrderDetailView(LoginRequiredMixin, TemplateView):
    template_name = 'financial/purchase_orders/purchase_order_detail.html'

    def get_context_data(self, **kwargs):
        context = super(PurchaseOrderDetailView, self).get_context_data(**kwargs)
        pk, user = self.kwargs['pk'], self.request.user
        context['purchase_order'] = PurchaseOrder.objects.get(pk=pk, user=user)
        context['item'] = PurchaseOrderItem.objects.filter(order=pk)
        return context